2000-02-18 03:38:33 +08:00
|
|
|
/* mmap.cc
|
|
|
|
|
2006-01-12 23:53:51 +08:00
|
|
|
Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
2006 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2001-01-16 03:42:33 +08:00
|
|
|
#include <unistd.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/mman.h>
|
2005-12-07 19:16:47 +08:00
|
|
|
#include <sys/param.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2001-04-17 01:26:04 +08:00
|
|
|
#include "cygheap.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "pinfo.h"
|
2001-07-11 05:04:14 +08:00
|
|
|
#include "sys/cygwin.h"
|
2005-02-26 05:23:15 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* __PROT_ATTACH indicates an anonymous mapping which is supposed to be
|
|
|
|
attached to a file mapping for pages beyond the file's EOF. The idea
|
|
|
|
is to support mappings longer than the file, without the file growing
|
|
|
|
to mapping length (POSIX semantics). */
|
|
|
|
#define __PROT_ATTACH 0x8000000
|
|
|
|
/* Filler pages are the pages from the last file backed page to the next
|
|
|
|
64K boundary. These pages are created as anonymous pages, but with
|
|
|
|
the same page protection as the file's pages, since POSIX applications
|
|
|
|
expect to be able to access this part the same way as the file pages. */
|
|
|
|
#define __PROT_FILLER 0x4000000
|
|
|
|
|
2002-06-27 21:01:35 +08:00
|
|
|
#define PAGE_CNT(bytes) howmany((bytes),getpagesize())
|
2001-01-16 03:42:33 +08:00
|
|
|
|
2002-09-23 08:31:31 +08:00
|
|
|
#define PGBITS (sizeof (DWORD)*8)
|
|
|
|
#define MAPSIZE(pages) howmany ((pages), PGBITS)
|
2001-01-16 03:42:33 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
#define MAP_SET(n) (page_map[(n)/PGBITS] |= (1L << ((n) % PGBITS)))
|
|
|
|
#define MAP_CLR(n) (page_map[(n)/PGBITS] &= ~(1L << ((n) % PGBITS)))
|
|
|
|
#define MAP_ISSET(n) (page_map[(n)/PGBITS] & (1L << ((n) % PGBITS)))
|
2001-01-16 03:42:33 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Used for anonymous mappings. */
|
|
|
|
static fhandler_dev_zero fh_anonymous;
|
|
|
|
/* Used for reopening a disk file when necessary. */
|
|
|
|
static fhandler_disk_file fh_disk_file;
|
2005-11-30 00:28:05 +08:00
|
|
|
|
|
|
|
/* Small helpers to avoid having lots of flag bit tests in the code. */
|
|
|
|
static inline bool
|
|
|
|
priv (int flags)
|
|
|
|
{
|
|
|
|
return (flags & MAP_PRIVATE) == MAP_PRIVATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
fixed (int flags)
|
|
|
|
{
|
|
|
|
return (flags & MAP_FIXED) == MAP_FIXED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
anonymous (int flags)
|
|
|
|
{
|
|
|
|
return (flags & MAP_ANONYMOUS) == MAP_ANONYMOUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
noreserve (int flags)
|
|
|
|
{
|
|
|
|
return (flags & MAP_NORESERVE) == MAP_NORESERVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
autogrow (int flags)
|
|
|
|
{
|
|
|
|
return (flags & MAP_AUTOGROW) == MAP_AUTOGROW;
|
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
static inline bool
|
|
|
|
attached (int prot)
|
|
|
|
{
|
|
|
|
return (prot & __PROT_ATTACH) == __PROT_ATTACH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
filler (int prot)
|
|
|
|
{
|
|
|
|
return (prot & __PROT_FILLER) == __PROT_FILLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline DWORD
|
|
|
|
gen_create_protect (DWORD openflags, int flags)
|
|
|
|
{
|
|
|
|
DWORD ret = PAGE_READONLY;
|
|
|
|
|
2005-12-12 18:00:32 +08:00
|
|
|
if (priv (flags))
|
|
|
|
ret = PAGE_WRITECOPY;
|
|
|
|
else if (openflags & GENERIC_WRITE)
|
|
|
|
ret = PAGE_READWRITE;
|
2005-12-07 19:16:47 +08:00
|
|
|
|
|
|
|
/* Ignore EXECUTE permission on 9x. */
|
|
|
|
if ((openflags & GENERIC_EXECUTE)
|
|
|
|
&& wincap.virtual_protect_works_on_shared_pages ())
|
|
|
|
ret <<= 4;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Generate Windows protection flags from mmap prot and flag values. */
|
2005-12-02 04:35:13 +08:00
|
|
|
static inline DWORD
|
2005-12-07 19:16:47 +08:00
|
|
|
gen_protect (int prot, int flags)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
DWORD ret = PAGE_NOACCESS;
|
2005-12-07 19:16:47 +08:00
|
|
|
|
|
|
|
/* Attached pages are only reserved, but the protection must be a
|
|
|
|
valid value, so we just return PAGE_READWRITE. */
|
|
|
|
if (attached (prot))
|
|
|
|
return PAGE_EXECUTE_READWRITE;
|
|
|
|
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
ret = (priv (flags) && (!anonymous (flags) || filler (prot)))
|
|
|
|
? PAGE_WRITECOPY : PAGE_READWRITE;
|
2005-11-29 06:32:29 +08:00
|
|
|
else if (prot & PROT_READ)
|
2005-12-07 19:16:47 +08:00
|
|
|
ret = PAGE_READONLY;
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Ignore EXECUTE permission on 9x. */
|
|
|
|
if ((prot & PROT_EXEC)
|
|
|
|
&& wincap.virtual_protect_works_on_shared_pages ())
|
|
|
|
ret <<= 4;
|
2005-12-07 19:16:47 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-30 00:28:05 +08:00
|
|
|
/* Generate Windows access flags from mmap prot and flag values.
|
|
|
|
Only used on 9x. PROT_EXEC not supported here since it's not
|
|
|
|
necessary. */
|
2005-12-02 04:35:13 +08:00
|
|
|
static inline DWORD
|
2005-12-07 19:16:47 +08:00
|
|
|
gen_access (DWORD openflags, int flags)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD ret = FILE_MAP_READ;
|
2005-11-30 00:28:05 +08:00
|
|
|
if (priv (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
ret = FILE_MAP_COPY;
|
2005-12-07 19:16:47 +08:00
|
|
|
else if (openflags & GENERIC_WRITE)
|
2005-11-30 00:28:05 +08:00
|
|
|
ret = priv (flags) ? FILE_MAP_COPY : FILE_MAP_WRITE;
|
2005-11-29 06:32:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-30 00:28:05 +08:00
|
|
|
/* OS specific wrapper functions for map/section functions. */
|
2005-11-29 06:32:29 +08:00
|
|
|
static BOOL
|
|
|
|
VirtualProt9x (PVOID addr, SIZE_T len, DWORD prot, PDWORD oldprot)
|
|
|
|
{
|
|
|
|
if (addr >= (caddr_t)0x80000000 && addr <= (caddr_t)0xBFFFFFFF)
|
|
|
|
return TRUE; /* FAKEALARM! */
|
|
|
|
return VirtualProtect (addr, len, prot, oldprot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
VirtualProtNT (PVOID addr, SIZE_T len, DWORD prot, PDWORD oldprot)
|
|
|
|
{
|
|
|
|
return VirtualProtect (addr, len, prot, oldprot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
VirtualProtEx9x (HANDLE parent, PVOID addr, SIZE_T len, DWORD prot,
|
|
|
|
PDWORD oldprot)
|
|
|
|
{
|
|
|
|
if (addr >= (caddr_t)0x80000000 && addr <= (caddr_t)0xBFFFFFFF)
|
|
|
|
return TRUE; /* FAKEALARM! */
|
|
|
|
return VirtualProtectEx (parent, addr, len, prot, oldprot);
|
|
|
|
}
|
|
|
|
static BOOL
|
|
|
|
VirtualProtExNT (HANDLE parent, PVOID addr, SIZE_T len, DWORD prot,
|
|
|
|
PDWORD oldprot)
|
|
|
|
{
|
|
|
|
return VirtualProtectEx (parent, addr, len, prot, oldprot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This allows to stay lazy about VirtualProtect usage in subsequent code. */
|
|
|
|
#define VirtualProtect(a,l,p,o) (mmap_func->VirtualProt((a),(l),(p),(o)))
|
|
|
|
#define VirtualProtectEx(h,a,l,p,o) (mmap_func->VirtualProtEx((h),(a),(l),(p),(o)))
|
|
|
|
|
|
|
|
static HANDLE
|
2005-12-07 19:16:47 +08:00
|
|
|
CreateMapping9x (HANDLE fhdl, size_t len, _off64_t off, DWORD openflags,
|
|
|
|
int prot, int flags, const char *name)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
DWORD high, low;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD protect = gen_create_protect (openflags, flags);
|
2005-11-29 06:32:29 +08:00
|
|
|
|
2005-11-30 00:28:05 +08:00
|
|
|
/* copy-on-write doesn't work properly on 9x with real files. While the
|
|
|
|
changes are not propagated to the file, they are visible to other
|
|
|
|
processes sharing the same file mapping object. Workaround: Don't
|
|
|
|
use named file mapping. That should work since sharing file
|
|
|
|
mappings only works reliable using named file mapping on 9x.
|
|
|
|
|
|
|
|
On 9x/ME try first to open the mapping by name when opening a
|
|
|
|
shared file object. This is needed since 9x/ME only shares objects
|
|
|
|
between processes by name. What a mess... */
|
|
|
|
|
|
|
|
if (fhdl != INVALID_HANDLE_VALUE && !priv (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
/* Grrr, the whole stuff is just needed to try to get a reliable
|
|
|
|
mapping of the same file. Even that uprising isn't bullet
|
|
|
|
proof but it does it's best... */
|
|
|
|
char namebuf[CYG_MAX_PATH];
|
|
|
|
cygwin_conv_to_full_posix_path (name, namebuf);
|
|
|
|
for (int i = strlen (namebuf) - 1; i >= 0; --i)
|
|
|
|
namebuf[i] = cyg_tolower (namebuf [i]);
|
|
|
|
|
|
|
|
debug_printf ("named sharing");
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD access = gen_access (openflags, flags);
|
|
|
|
/* Different access modes result in incompatible mappings. So we
|
2005-12-22 13:57:54 +08:00
|
|
|
create different maps per access mode by using different names. */
|
2005-12-07 19:16:47 +08:00
|
|
|
switch (access)
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
case FILE_MAP_READ:
|
|
|
|
namebuf[0] = 'R';
|
|
|
|
break;
|
|
|
|
case FILE_MAP_WRITE:
|
|
|
|
namebuf[0] = 'W';
|
|
|
|
break;
|
|
|
|
case FILE_MAP_COPY:
|
|
|
|
namebuf[0] = 'C';
|
|
|
|
break;
|
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!(h = OpenFileMapping (access, TRUE, namebuf)))
|
2005-12-07 19:16:47 +08:00
|
|
|
h = CreateFileMapping (fhdl, &sec_none, protect, 0, 0, namebuf);
|
2005-11-29 06:32:29 +08:00
|
|
|
}
|
|
|
|
else if (fhdl == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Standard anonymous mapping needs non-zero len. */
|
|
|
|
h = CreateFileMapping (fhdl, &sec_none, protect, 0, len, NULL);
|
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
else if (autogrow (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
high = (off + len) >> 32;
|
|
|
|
low = (off + len) & UINT32_MAX;
|
|
|
|
/* Auto-grow only works if the protection is PAGE_READWRITE. So,
|
2005-12-22 13:57:54 +08:00
|
|
|
first we call CreateFileMapping with PAGE_READWRITE, then, if the
|
2005-11-29 06:32:29 +08:00
|
|
|
requested protection is different, we close the mapping and
|
|
|
|
reopen it again with the correct protection, if auto-grow worked. */
|
|
|
|
h = CreateFileMapping (fhdl, &sec_none, PAGE_READWRITE,
|
|
|
|
high, low, NULL);
|
|
|
|
if (h && protect != PAGE_READWRITE)
|
|
|
|
{
|
|
|
|
CloseHandle (h);
|
|
|
|
h = CreateFileMapping (fhdl, &sec_none, protect,
|
|
|
|
high, low, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Zero len creates mapping for whole file. */
|
|
|
|
h = CreateFileMapping (fhdl, &sec_none, protect, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE
|
2005-12-07 19:16:47 +08:00
|
|
|
CreateMappingNT (HANDLE fhdl, size_t len, _off64_t off, DWORD openflags,
|
|
|
|
int prot, int flags, const char *)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
LARGE_INTEGER sectionsize = { QuadPart: len };
|
2005-12-07 19:16:47 +08:00
|
|
|
ULONG protect = gen_create_protect (openflags, flags);
|
|
|
|
ULONG attributes = attached (prot) ? SEC_RESERVE : SEC_COMMIT;
|
2005-11-29 06:32:29 +08:00
|
|
|
|
|
|
|
OBJECT_ATTRIBUTES oa;
|
|
|
|
InitializeObjectAttributes (&oa, NULL, OBJ_INHERIT, NULL,
|
|
|
|
sec_none.lpSecurityDescriptor);
|
|
|
|
|
|
|
|
if (fhdl == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Standard anonymous mapping needs non-zero len. */
|
|
|
|
ret = NtCreateSection (&h, SECTION_ALL_ACCESS, &oa,
|
|
|
|
§ionsize, protect, attributes, NULL);
|
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
else if (autogrow (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
/* Auto-grow only works if the protection is PAGE_READWRITE. So,
|
2005-12-22 13:57:54 +08:00
|
|
|
first we call NtCreateSection with PAGE_READWRITE, then, if the
|
2005-11-29 06:32:29 +08:00
|
|
|
requested protection is different, we close the mapping and
|
|
|
|
reopen it again with the correct protection, if auto-grow worked. */
|
|
|
|
sectionsize.QuadPart += off;
|
|
|
|
ret = NtCreateSection (&h, SECTION_ALL_ACCESS, &oa,
|
|
|
|
§ionsize, PAGE_READWRITE, attributes, fhdl);
|
|
|
|
if (NT_SUCCESS (ret) && protect != PAGE_READWRITE)
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
CloseHandle (h);
|
|
|
|
ret = NtCreateSection (&h, SECTION_ALL_ACCESS, &oa,
|
|
|
|
§ionsize, protect, attributes, fhdl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Zero len creates mapping for whole file and allows
|
2005-12-22 13:57:54 +08:00
|
|
|
AT_EXTENDABLE_FILE mapping, if we ever use it... */
|
2005-11-29 06:32:29 +08:00
|
|
|
sectionsize.QuadPart = 0;
|
|
|
|
ret = NtCreateSection (&h, SECTION_ALL_ACCESS, &oa,
|
|
|
|
§ionsize, protect, attributes, fhdl);
|
|
|
|
}
|
|
|
|
if (!NT_SUCCESS (ret))
|
|
|
|
{
|
|
|
|
h = NULL;
|
|
|
|
SetLastError (RtlNtStatusToDosError (ret));
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2005-12-07 19:16:47 +08:00
|
|
|
MapView9x (HANDLE h, void *addr, size_t len, DWORD openflags,
|
|
|
|
int prot, int flags, _off64_t off)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
DWORD high = off >> 32;
|
|
|
|
DWORD low = off & UINT32_MAX;
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD access = gen_access (openflags, flags);
|
2005-11-29 06:32:29 +08:00
|
|
|
void *base;
|
|
|
|
|
|
|
|
/* Try mapping using the given address first, even if it's NULL.
|
|
|
|
If it failed, and addr was not NULL and flags is not MAP_FIXED,
|
|
|
|
try again with NULL address. */
|
|
|
|
if (!addr)
|
|
|
|
base = MapViewOfFile (h, access, high, low, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base = MapViewOfFileEx (h, access, high, low, len, addr);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!base && !fixed (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
base = MapViewOfFile (h, access, high, low, len);
|
|
|
|
}
|
|
|
|
debug_printf ("%x = MapViewOfFileEx (h:%x, access:%x, 0, off:%D, "
|
|
|
|
"len:%u, addr:%x)", base, h, access, off, len, addr);
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2005-12-07 19:16:47 +08:00
|
|
|
MapViewNT (HANDLE h, void *addr, size_t len, DWORD openflags,
|
|
|
|
int prot, int flags, _off64_t off)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
LARGE_INTEGER offset = { QuadPart:off };
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD protect = gen_create_protect (openflags, flags);
|
2005-11-29 06:32:29 +08:00
|
|
|
void *base = addr;
|
2005-12-07 19:16:47 +08:00
|
|
|
ULONG commitsize = attached (prot) ? 0 : len;
|
|
|
|
ULONG viewsize = len;
|
2006-01-11 02:11:32 +08:00
|
|
|
ULONG alloc_type = base && !wincap.is_wow64 () ? AT_ROUND_TO_PAGE : 0;
|
2005-11-29 06:32:29 +08:00
|
|
|
|
|
|
|
/* Try mapping using the given address first, even if it's NULL.
|
|
|
|
If it failed, and addr was not NULL and flags is not MAP_FIXED,
|
|
|
|
try again with NULL address. */
|
2005-12-07 19:16:47 +08:00
|
|
|
ret = NtMapViewOfSection (h, GetCurrentProcess (), &base, 0, commitsize,
|
|
|
|
&offset, &viewsize, ViewShare, alloc_type, protect);
|
2005-12-02 04:35:13 +08:00
|
|
|
if (!NT_SUCCESS (ret) && addr && !fixed (flags))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
base = NULL;
|
2005-12-07 19:16:47 +08:00
|
|
|
ret = NtMapViewOfSection (h, GetCurrentProcess (), &base, 0, commitsize,
|
2005-12-22 13:57:54 +08:00
|
|
|
&offset, &viewsize, ViewShare, 0, protect);
|
2005-11-29 06:32:29 +08:00
|
|
|
}
|
|
|
|
if (!NT_SUCCESS (ret))
|
|
|
|
{
|
|
|
|
base = NULL;
|
|
|
|
SetLastError (RtlNtStatusToDosError (ret));
|
|
|
|
}
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("%x = NtMapViewOfSection (h:%x, addr:%x, len:%u, off:%D, "
|
|
|
|
"protect:%x, type:%x)", base, h, addr, len, off, protect, 0);
|
2005-11-29 06:32:29 +08:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mmap_func_t
|
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
HANDLE (*CreateMapping)(HANDLE, size_t, _off64_t, DWORD, int, int,
|
2005-12-22 13:57:54 +08:00
|
|
|
const char *);
|
2005-12-07 19:16:47 +08:00
|
|
|
void * (*MapView)(HANDLE, void *, size_t, DWORD, int, int, _off64_t);
|
2005-11-29 06:32:29 +08:00
|
|
|
BOOL (*VirtualProt)(PVOID, SIZE_T, DWORD, PDWORD);
|
|
|
|
BOOL (*VirtualProtEx)(HANDLE, PVOID, SIZE_T, DWORD, PDWORD);
|
|
|
|
};
|
|
|
|
|
2005-12-06 04:20:18 +08:00
|
|
|
mmap_func_t mmap_funcs_9x =
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
CreateMapping9x,
|
|
|
|
MapView9x,
|
|
|
|
VirtualProt9x,
|
|
|
|
VirtualProtEx9x
|
|
|
|
};
|
|
|
|
|
2005-12-06 04:20:18 +08:00
|
|
|
mmap_func_t mmap_funcs_nt =
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
CreateMappingNT,
|
|
|
|
MapViewNT,
|
|
|
|
VirtualProtNT,
|
|
|
|
VirtualProtExNT
|
|
|
|
};
|
|
|
|
|
|
|
|
mmap_func_t *mmap_func;
|
|
|
|
|
|
|
|
void
|
|
|
|
mmap_init ()
|
|
|
|
{
|
|
|
|
mmap_func = wincap.is_winnt () ? &mmap_funcs_nt : &mmap_funcs_9x;
|
|
|
|
}
|
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
/* Class structure used to keep a record of all current mmap areas
|
|
|
|
in a process. Needed for bookkeeping all mmaps in a process and
|
|
|
|
for duplicating all mmaps after fork() since mmaps are not propagated
|
|
|
|
to child processes by Windows. All information must be duplicated
|
|
|
|
by hand, see fixup_mmaps_after_fork().
|
2003-11-29 04:55:59 +08:00
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
The class structure:
|
2003-11-29 04:55:59 +08:00
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
One member of class map per process, global variable mmapped_areas.
|
|
|
|
Contains a dynamic class list array. Each list entry represents all
|
|
|
|
mapping to a file, keyed by file descriptor and file name hash.
|
|
|
|
Each list entry contains a dynamic class mmap_record array. Each
|
|
|
|
mmap_record represents exactly one mapping. For each mapping, there's
|
|
|
|
an additional so called `page_map'. It's an array of bits, one bit
|
|
|
|
per mapped memory page. The bit is set if the page is accessible,
|
|
|
|
unset otherwise. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
class mmap_record
|
|
|
|
{
|
|
|
|
private:
|
2005-11-29 06:32:29 +08:00
|
|
|
int fd;
|
|
|
|
HANDLE mapping_hdl;
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD openflags;
|
2005-11-29 06:32:29 +08:00
|
|
|
int prot;
|
|
|
|
int flags;
|
|
|
|
_off64_t offset;
|
|
|
|
DWORD len;
|
|
|
|
caddr_t base_address;
|
|
|
|
DWORD *page_map;
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
device dev;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
public:
|
2005-12-07 19:16:47 +08:00
|
|
|
mmap_record (int nfd, HANDLE h, DWORD of, int p, int f, _off64_t o, DWORD l,
|
2005-12-22 13:57:54 +08:00
|
|
|
caddr_t b) :
|
2005-11-29 06:32:29 +08:00
|
|
|
fd (nfd),
|
|
|
|
mapping_hdl (h),
|
2005-12-07 19:16:47 +08:00
|
|
|
openflags (of),
|
2005-11-29 06:32:29 +08:00
|
|
|
prot (p),
|
|
|
|
flags (f),
|
|
|
|
offset (o),
|
|
|
|
len (l),
|
|
|
|
base_address (b),
|
|
|
|
page_map (NULL)
|
2001-04-17 01:26:04 +08:00
|
|
|
{
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
dev.devn = 0;
|
2001-09-03 10:13:05 +08:00
|
|
|
if (fd >= 0 && !cygheap->fdtab.not_open (fd))
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
dev = cygheap->fdtab[fd]->dev ();
|
2005-11-30 00:28:05 +08:00
|
|
|
else if (fd == -1)
|
|
|
|
dev.parse (FH_ZERO);
|
2001-04-17 01:26:04 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
int get_fd () const { return fd; }
|
|
|
|
HANDLE get_handle () const { return mapping_hdl; }
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
device& get_device () { return dev; }
|
2005-11-29 06:32:29 +08:00
|
|
|
int get_prot () const { return prot; }
|
2005-12-07 19:16:47 +08:00
|
|
|
int get_openflags () const { return openflags; }
|
2005-11-29 06:32:29 +08:00
|
|
|
int get_flags () const { return flags; }
|
2005-11-30 00:28:05 +08:00
|
|
|
bool priv () const { return ::priv (flags); }
|
|
|
|
bool fixed () const { return ::fixed (flags); }
|
|
|
|
bool anonymous () const { return ::anonymous (flags); }
|
|
|
|
bool noreserve () const { return ::noreserve (flags); }
|
|
|
|
bool autogrow () const { return ::autogrow (flags); }
|
2005-12-07 19:16:47 +08:00
|
|
|
bool attached () const { return ::attached (prot); }
|
|
|
|
bool filler () const { return ::filler (prot); }
|
2005-11-29 06:32:29 +08:00
|
|
|
_off64_t get_offset () const { return offset; }
|
|
|
|
DWORD get_len () const { return len; }
|
|
|
|
caddr_t get_address () const { return base_address; }
|
2003-09-04 18:27:51 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
bool alloc_page_map ();
|
|
|
|
void free_page_map () { if (page_map) cfree (page_map); }
|
2003-09-04 18:27:51 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD find_unused_pages (DWORD pages) const;
|
2003-09-04 18:27:51 +08:00
|
|
|
_off64_t map_pages (_off64_t off, DWORD len);
|
2005-02-25 19:15:22 +08:00
|
|
|
bool map_pages (caddr_t addr, DWORD len);
|
2003-12-08 06:37:12 +08:00
|
|
|
bool unmap_pages (caddr_t addr, DWORD len);
|
2003-01-15 04:40:09 +08:00
|
|
|
int access (caddr_t address);
|
2001-04-17 01:26:04 +08:00
|
|
|
|
|
|
|
fhandler_base *alloc_fh ();
|
|
|
|
void free_fh (fhandler_base *fh);
|
2005-12-22 13:57:54 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD gen_create_protect () const
|
|
|
|
{ return ::gen_create_protect (get_openflags (), get_flags ()); }
|
|
|
|
DWORD gen_protect () const
|
|
|
|
{ return ::gen_protect (get_prot (), get_flags ()); }
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD gen_access () const
|
2005-12-07 19:16:47 +08:00
|
|
|
{ return ::gen_access (get_openflags (), get_flags ()); }
|
2005-11-29 06:32:29 +08:00
|
|
|
bool compatible_flags (int fl) const;
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
class list
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
mmap_record *recs;
|
|
|
|
int nrecs, maxrecs;
|
|
|
|
int fd;
|
|
|
|
DWORD hash;
|
|
|
|
|
|
|
|
public:
|
|
|
|
int get_fd () const { return fd; }
|
|
|
|
DWORD get_hash () const { return hash; }
|
|
|
|
mmap_record *get_record (int i) { return i >= nrecs ? NULL : recs + i; }
|
|
|
|
|
2005-11-30 00:28:05 +08:00
|
|
|
bool anonymous () const { return fd == -1; }
|
2003-09-04 18:27:51 +08:00
|
|
|
void set (int nfd);
|
2005-11-29 06:32:29 +08:00
|
|
|
mmap_record *add_record (mmap_record r);
|
2003-09-04 18:27:51 +08:00
|
|
|
bool del_record (int i);
|
|
|
|
void free_recs () { if (recs) cfree (recs); }
|
|
|
|
mmap_record *search_record (_off64_t off, DWORD len);
|
|
|
|
long search_record (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len,
|
|
|
|
long start);
|
2005-11-30 23:05:48 +08:00
|
|
|
caddr_t try_map (void *addr, size_t len, int flags, _off64_t off);
|
2003-09-04 18:27:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class map
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
list *lists;
|
2005-11-29 06:32:29 +08:00
|
|
|
unsigned nlists, maxlists;
|
2003-09-04 18:27:51 +08:00
|
|
|
|
|
|
|
public:
|
2005-11-29 06:32:29 +08:00
|
|
|
list *get_list (unsigned i) { return i >= nlists ? NULL : lists + i; }
|
2003-09-04 18:27:51 +08:00
|
|
|
list *get_list_by_fd (int fd);
|
|
|
|
list *add_list (int fd);
|
2005-11-29 06:32:29 +08:00
|
|
|
void del_list (unsigned i);
|
2003-09-04 18:27:51 +08:00
|
|
|
};
|
|
|
|
|
2005-12-02 04:35:13 +08:00
|
|
|
/* This is the global map structure pointer. */
|
2005-11-29 06:32:29 +08:00
|
|
|
static map mmapped_areas;
|
|
|
|
|
|
|
|
bool
|
|
|
|
mmap_record::compatible_flags (int fl) const
|
|
|
|
{
|
|
|
|
#define MAP_COMPATMASK (MAP_TYPE | MAP_NORESERVE)
|
|
|
|
return (get_flags () & MAP_COMPATMASK) == (fl & MAP_COMPATMASK);
|
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
|
2001-01-16 03:42:33 +08:00
|
|
|
DWORD
|
2005-11-29 06:32:29 +08:00
|
|
|
mmap_record::find_unused_pages (DWORD pages) const
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD mapped_pages = PAGE_CNT (get_len ());
|
2001-01-16 03:42:33 +08:00
|
|
|
DWORD start;
|
|
|
|
|
2001-11-06 04:58:22 +08:00
|
|
|
if (pages > mapped_pages)
|
|
|
|
return (DWORD)-1;
|
2001-01-29 17:25:10 +08:00
|
|
|
for (start = 0; start <= mapped_pages - pages; ++start)
|
2001-01-16 03:42:33 +08:00
|
|
|
if (!MAP_ISSET (start))
|
|
|
|
{
|
2001-09-03 10:13:05 +08:00
|
|
|
DWORD cnt;
|
|
|
|
for (cnt = 0; cnt < pages; ++cnt)
|
2001-01-16 03:42:33 +08:00
|
|
|
if (MAP_ISSET (start + cnt))
|
|
|
|
break;
|
2001-09-03 10:13:05 +08:00
|
|
|
if (cnt >= pages)
|
2001-01-16 03:42:33 +08:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
return (DWORD)-1;
|
|
|
|
}
|
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
bool
|
2005-11-29 06:32:29 +08:00
|
|
|
mmap_record::alloc_page_map ()
|
2003-02-08 04:57:30 +08:00
|
|
|
{
|
|
|
|
/* Allocate one bit per page */
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!(page_map = (DWORD *) ccalloc (HEAP_MMAP,
|
|
|
|
MAPSIZE (PAGE_CNT (get_len ())),
|
|
|
|
sizeof (DWORD))))
|
2003-09-04 18:27:51 +08:00
|
|
|
return false;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
DWORD start_protect = gen_create_protect ();
|
2005-12-02 04:35:13 +08:00
|
|
|
DWORD real_protect = gen_protect ();
|
|
|
|
if (real_protect != start_protect
|
|
|
|
&& !VirtualProtect (get_address (), get_len (),
|
|
|
|
real_protect, &start_protect))
|
|
|
|
system_printf ("Warning: VirtualProtect (addr: %p, len: 0x%x, "
|
|
|
|
"new_prot: 0x%x, old_prot: 0x%x), %E",
|
|
|
|
get_address (), get_len (),
|
|
|
|
real_protect, start_protect);
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD len = PAGE_CNT (get_len ());
|
2004-07-16 22:32:03 +08:00
|
|
|
while (len-- > 0)
|
2005-11-29 06:32:29 +08:00
|
|
|
MAP_SET (len);
|
2003-09-04 18:27:51 +08:00
|
|
|
return true;
|
2003-02-08 04:57:30 +08:00
|
|
|
}
|
|
|
|
|
2003-04-02 00:11:41 +08:00
|
|
|
_off64_t
|
2003-09-04 18:27:51 +08:00
|
|
|
mmap_record::map_pages (_off64_t off, DWORD len)
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
2003-02-08 04:57:30 +08:00
|
|
|
/* Used ONLY if this mapping matches into the chunk of another already
|
|
|
|
performed mapping in a special case of MAP_ANON|MAP_PRIVATE.
|
2003-03-10 04:31:07 +08:00
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
Otherwise it's job is now done by alloc_page_map(). */
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD old_prot;
|
|
|
|
debug_printf ("map_pages (fd=%d, off=%D, len=%u)", get_fd (), off, len);
|
2001-01-16 03:42:33 +08:00
|
|
|
len = PAGE_CNT (len);
|
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
if ((off = find_unused_pages (len)) == (DWORD)-1)
|
2003-02-08 04:57:30 +08:00
|
|
|
return 0L;
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!noreserve ()
|
|
|
|
&& !VirtualProtect (get_address () + off * getpagesize (),
|
|
|
|
len * getpagesize (), gen_protect (), &old_prot))
|
2002-03-14 04:54:57 +08:00
|
|
|
{
|
2002-09-24 22:17:50 +08:00
|
|
|
__seterrno ();
|
2003-04-02 00:11:41 +08:00
|
|
|
return (_off64_t)-1;
|
2002-03-14 04:54:57 +08:00
|
|
|
}
|
2001-01-16 03:42:33 +08:00
|
|
|
|
2003-02-08 04:57:30 +08:00
|
|
|
while (len-- > 0)
|
|
|
|
MAP_SET (off + len);
|
|
|
|
return off * getpagesize ();
|
2001-01-16 03:42:33 +08:00
|
|
|
}
|
|
|
|
|
2005-02-25 19:15:22 +08:00
|
|
|
bool
|
|
|
|
mmap_record::map_pages (caddr_t addr, DWORD len)
|
|
|
|
{
|
|
|
|
debug_printf ("map_pages (addr=%x, len=%u)", addr, len);
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD old_prot;
|
|
|
|
DWORD off = addr - get_address ();
|
2005-02-25 19:15:22 +08:00
|
|
|
off /= getpagesize ();
|
|
|
|
len = PAGE_CNT (len);
|
|
|
|
/* First check if the area is unused right now. */
|
|
|
|
for (DWORD l = 0; l < len; ++l)
|
|
|
|
if (MAP_ISSET (off + l))
|
|
|
|
{
|
2005-05-02 11:50:11 +08:00
|
|
|
set_errno (EINVAL);
|
2005-02-25 19:15:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!noreserve ()
|
|
|
|
&& !VirtualProtect (get_address () + off * getpagesize (),
|
|
|
|
len * getpagesize (), gen_protect (), &old_prot))
|
2005-02-25 19:15:22 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; len-- > 0; ++off)
|
|
|
|
MAP_SET (off);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
bool
|
2003-09-04 18:27:51 +08:00
|
|
|
mmap_record::unmap_pages (caddr_t addr, DWORD len)
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
|
|
|
DWORD old_prot;
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD off = addr - get_address ();
|
2001-01-16 03:42:33 +08:00
|
|
|
off /= getpagesize ();
|
|
|
|
len = PAGE_CNT (len);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (anonymous () && priv () && noreserve ()
|
|
|
|
&& !VirtualFree (get_address () + off * getpagesize (),
|
|
|
|
len * getpagesize (), MEM_DECOMMIT))
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("VirtualFree in unmap_pages () failed, %E");
|
2005-11-30 00:28:05 +08:00
|
|
|
else if (!VirtualProtect (get_address () + off * getpagesize (),
|
|
|
|
len * getpagesize (), PAGE_NOACCESS, &old_prot))
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("VirtualProtect in unmap_pages () failed, %E");
|
2001-01-16 03:42:33 +08:00
|
|
|
|
|
|
|
for (; len-- > 0; ++off)
|
|
|
|
MAP_CLR (off);
|
|
|
|
/* Return TRUE if all pages are free'd which may result in unmapping
|
|
|
|
the whole chunk. */
|
2005-11-29 06:32:29 +08:00
|
|
|
for (len = MAPSIZE (PAGE_CNT (get_len ())); len > 0; )
|
|
|
|
if (page_map[--len])
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2001-01-16 03:42:33 +08:00
|
|
|
}
|
|
|
|
|
2002-03-12 01:57:22 +08:00
|
|
|
int
|
2003-01-15 04:40:09 +08:00
|
|
|
mmap_record::access (caddr_t address)
|
2002-03-12 01:57:22 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
if (address < get_address () || address >= get_address () + get_len ())
|
2002-03-12 01:57:22 +08:00
|
|
|
return 0;
|
2005-11-29 06:32:29 +08:00
|
|
|
DWORD off = (address - get_address ()) / getpagesize ();
|
2002-03-12 01:57:22 +08:00
|
|
|
return MAP_ISSET (off);
|
|
|
|
}
|
|
|
|
|
2001-04-17 01:26:04 +08:00
|
|
|
fhandler_base *
|
|
|
|
mmap_record::alloc_fh ()
|
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
if (anonymous ())
|
2001-04-17 01:26:04 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
fh_anonymous.set_io_handle (INVALID_HANDLE_VALUE);
|
|
|
|
fh_anonymous.set_access (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);
|
|
|
|
return &fh_anonymous;
|
2001-04-17 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The file descriptor could have been closed or, even
|
|
|
|
worse, could have been reused for another file before
|
|
|
|
the call to fork(). This requires creating a fhandler
|
|
|
|
of the correct type to be sure to call the method of the
|
|
|
|
correct class. */
|
2005-12-07 19:16:47 +08:00
|
|
|
fhandler_base *fh = build_fh_dev (get_device ());
|
|
|
|
fh->set_access (get_openflags ());
|
|
|
|
return fh;
|
2001-04-17 01:26:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mmap_record::free_fh (fhandler_base *fh)
|
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!anonymous ())
|
2001-04-17 01:26:04 +08:00
|
|
|
cfree (fh);
|
|
|
|
}
|
|
|
|
|
2001-01-16 03:42:33 +08:00
|
|
|
mmap_record *
|
2005-11-29 06:32:29 +08:00
|
|
|
list::add_record (mmap_record r)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (nrecs == maxrecs)
|
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
mmap_record *new_recs;
|
|
|
|
if (maxrecs == 0)
|
2003-11-29 04:55:59 +08:00
|
|
|
new_recs = (mmap_record *)
|
2003-09-04 18:27:51 +08:00
|
|
|
cmalloc (HEAP_MMAP, 5 * sizeof (mmap_record));
|
|
|
|
else
|
|
|
|
new_recs = (mmap_record *)
|
|
|
|
crealloc (recs, (maxrecs + 5) * sizeof (mmap_record));
|
|
|
|
if (!new_recs)
|
2003-11-29 04:55:59 +08:00
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
maxrecs += 5;
|
2003-09-04 18:27:51 +08:00
|
|
|
recs = new_recs;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-01-16 03:42:33 +08:00
|
|
|
recs[nrecs] = r;
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!recs[nrecs].alloc_page_map ())
|
2003-09-04 18:27:51 +08:00
|
|
|
return NULL;
|
2001-01-16 03:42:33 +08:00
|
|
|
return recs + nrecs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used in mmap() */
|
|
|
|
mmap_record *
|
2003-09-04 18:27:51 +08:00
|
|
|
list::search_record (_off64_t off, DWORD len)
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
if (anonymous () && !off)
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
|
|
|
len = PAGE_CNT (len);
|
|
|
|
for (int i = 0; i < nrecs; ++i)
|
2003-09-04 18:27:51 +08:00
|
|
|
if (recs[i].find_unused_pages (len) != (DWORD)-1)
|
2001-01-16 03:42:33 +08:00
|
|
|
return recs + i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nrecs; ++i)
|
|
|
|
if (off >= recs[i].get_offset ()
|
2002-06-27 21:01:35 +08:00
|
|
|
&& off + len <= recs[i].get_offset ()
|
2005-11-29 06:32:29 +08:00
|
|
|
+ (PAGE_CNT (recs[i].get_len ()) * getpagesize ()))
|
2001-01-16 03:42:33 +08:00
|
|
|
return recs + i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used in munmap() */
|
2002-02-26 01:47:51 +08:00
|
|
|
long
|
2003-09-04 18:27:51 +08:00
|
|
|
list::search_record (caddr_t addr, DWORD len, caddr_t &m_addr, DWORD &m_len,
|
2004-07-15 17:21:16 +08:00
|
|
|
long start)
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
2003-07-06 02:59:35 +08:00
|
|
|
caddr_t low, high;
|
|
|
|
|
2004-07-15 17:21:16 +08:00
|
|
|
for (long i = start + 1; i < nrecs; ++i)
|
2003-07-06 02:59:35 +08:00
|
|
|
{
|
|
|
|
low = (addr >= recs[i].get_address ()) ? addr : recs[i].get_address ();
|
2005-12-07 19:16:47 +08:00
|
|
|
high = recs[i].get_address ();
|
|
|
|
if (recs[i].filler ())
|
2005-12-22 13:57:54 +08:00
|
|
|
high += recs[i].get_len ();
|
2005-12-07 19:16:47 +08:00
|
|
|
else
|
|
|
|
high += (PAGE_CNT (recs[i].get_len ()) * getpagesize ());
|
2003-07-06 02:59:35 +08:00
|
|
|
high = (addr + len < high) ? addr + len : high;
|
|
|
|
if (low < high)
|
|
|
|
{
|
|
|
|
m_addr = low;
|
|
|
|
m_len = high - low;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2002-02-26 01:47:51 +08:00
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-09-04 18:27:51 +08:00
|
|
|
list::set (int nfd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
fd = nfd;
|
|
|
|
if (!anonymous ())
|
2005-12-07 19:16:47 +08:00
|
|
|
{
|
|
|
|
/* The fd isn't sufficient since it could already be the fd of another
|
|
|
|
file. So we use the inode number as evaluated by fstat to identify
|
|
|
|
the file. */
|
|
|
|
struct stat st;
|
|
|
|
fstat (nfd, &st);
|
|
|
|
hash = st.st_ino;
|
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
nrecs = maxrecs = 0;
|
|
|
|
recs = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-04 18:27:51 +08:00
|
|
|
bool
|
|
|
|
list::del_record (int i)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
if (i < nrecs)
|
|
|
|
{
|
|
|
|
recs[i].free_page_map ();
|
|
|
|
for (; i < nrecs - 1; i++)
|
|
|
|
recs[i] = recs[i + 1];
|
|
|
|
nrecs--;
|
|
|
|
}
|
|
|
|
/* Return true if the list is empty which allows the caller to remove
|
|
|
|
this list from the list array. */
|
|
|
|
return !nrecs;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-11-30 23:05:48 +08:00
|
|
|
caddr_t
|
|
|
|
list::try_map (void *addr, size_t len, int flags, _off64_t off)
|
|
|
|
{
|
|
|
|
mmap_record *rec;
|
|
|
|
|
|
|
|
if (off == 0 && !fixed (flags))
|
|
|
|
{
|
|
|
|
/* If MAP_FIXED isn't given, check if this mapping matches into the
|
|
|
|
chunk of another already performed mapping. */
|
|
|
|
if ((rec = search_record (off, len)) != NULL
|
|
|
|
&& rec->compatible_flags (flags))
|
|
|
|
{
|
|
|
|
if ((off = rec->map_pages (off, len)) == (_off64_t)-1)
|
|
|
|
return (caddr_t) MAP_FAILED;
|
|
|
|
return (caddr_t) rec->get_address () + off;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fixed (flags))
|
|
|
|
{
|
|
|
|
/* If MAP_FIXED is given, test if the requested area is in an
|
|
|
|
unmapped part of an still active mapping. This can happen
|
|
|
|
if a memory region is unmapped and remapped with MAP_FIXED. */
|
|
|
|
caddr_t u_addr;
|
|
|
|
DWORD u_len;
|
|
|
|
long record_idx = -1;
|
|
|
|
if ((record_idx = search_record ((caddr_t) addr, len, u_addr, u_len,
|
|
|
|
record_idx)) >= 0)
|
|
|
|
{
|
|
|
|
rec = get_record (record_idx);
|
|
|
|
if (u_addr > (caddr_t) addr || u_addr + len < (caddr_t) addr + len
|
|
|
|
|| !rec->compatible_flags (flags))
|
|
|
|
{
|
|
|
|
/* Partial match only, or access mode doesn't match. */
|
|
|
|
/* FIXME: Handle partial mappings gracefully if adjacent
|
|
|
|
memory is available. */
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return (caddr_t) MAP_FAILED;
|
|
|
|
}
|
|
|
|
if (!rec->map_pages ((caddr_t) addr, len))
|
|
|
|
return (caddr_t) MAP_FAILED;
|
|
|
|
return (caddr_t) addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
list *
|
|
|
|
map::get_list_by_fd (int fd)
|
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < nlists; i++)
|
2005-12-07 19:16:47 +08:00
|
|
|
{
|
|
|
|
if (fd == -1 && lists[i].anonymous ())
|
|
|
|
return lists + i;
|
|
|
|
/* The fd isn't sufficient since it could already be the fd of another
|
|
|
|
file. So we use the inode number as evaluated by fstat to identify
|
|
|
|
the file. */
|
|
|
|
struct stat st;
|
|
|
|
if (fd != -1 && !fstat (fd, &st) && lists[i].get_hash () == st.st_ino)
|
|
|
|
return lists + i;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
list *
|
2003-09-04 18:27:51 +08:00
|
|
|
map::add_list (int fd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (nlists == maxlists)
|
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
list *new_lists;
|
|
|
|
if (maxlists == 0)
|
2003-11-29 04:55:59 +08:00
|
|
|
new_lists = (list *) cmalloc (HEAP_MMAP, 5 * sizeof (list));
|
2003-09-04 18:27:51 +08:00
|
|
|
else
|
|
|
|
new_lists = (list *) crealloc (lists, (maxlists + 5) * sizeof (list));
|
|
|
|
if (!new_lists)
|
2003-11-29 04:55:59 +08:00
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
maxlists += 5;
|
2003-09-04 18:27:51 +08:00
|
|
|
lists = new_lists;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
lists[nlists].set (fd);
|
|
|
|
return lists + nlists++;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-11-29 06:32:29 +08:00
|
|
|
map::del_list (unsigned i)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
if (i < nlists)
|
|
|
|
{
|
|
|
|
lists[i].free_recs ();
|
|
|
|
for (; i < nlists - 1; i++)
|
2003-11-29 04:55:59 +08:00
|
|
|
lists[i] = lists[i + 1];
|
2003-09-04 18:27:51 +08:00
|
|
|
nlists--;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* This function is called from exception_handler when a segmentation
|
|
|
|
violation has happened. The function should return true, if the
|
|
|
|
faulting address (the parameter) is within attached pages. In this
|
|
|
|
case the exception_handler raises SIGBUS, as demanded by the memory
|
|
|
|
protection extension described in SUSv3 (see the mmap man page).
|
|
|
|
If false is returned, a normal SIGSEGV is raised. */
|
|
|
|
bool
|
|
|
|
mmap_is_attached_page (ULONG_PTR addr)
|
|
|
|
{
|
|
|
|
list *map_list;
|
|
|
|
long record_idx;
|
|
|
|
caddr_t u_addr;
|
|
|
|
DWORD u_len;
|
|
|
|
DWORD pagesize = getsystempagesize ();
|
|
|
|
|
|
|
|
addr = rounddown (addr, pagesize);
|
|
|
|
if (!(map_list = mmapped_areas.get_list_by_fd (-1)))
|
|
|
|
return false;
|
|
|
|
if ((record_idx = map_list->search_record ((caddr_t)addr, pagesize,
|
|
|
|
u_addr, u_len, -1)) < 0)
|
|
|
|
return false;
|
|
|
|
if (!map_list->get_record (record_idx)->attached ())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static caddr_t
|
|
|
|
mmap_worker (fhandler_base *fh, caddr_t base, size_t len, int prot, int flags,
|
|
|
|
int fd, _off64_t off)
|
|
|
|
{
|
|
|
|
list *map_list;
|
|
|
|
HANDLE h = fh->mmap (&base, len, prot, flags, off);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
return NULL;
|
|
|
|
if (!(map_list = mmapped_areas.get_list_by_fd (fd))
|
|
|
|
&& !(map_list = mmapped_areas.add_list (fd)))
|
|
|
|
{
|
|
|
|
fh->munmap (h, base, len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mmap_record mmap_rec (fd, h, fh->get_access (), prot, flags, off, len, base);
|
|
|
|
mmap_record *rec = map_list->add_record (mmap_rec);
|
|
|
|
if (!rec)
|
|
|
|
{
|
|
|
|
fh->munmap (h, base, len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2003-09-09 17:41:18 +08:00
|
|
|
extern "C" void *
|
|
|
|
mmap64 (void *addr, size_t len, int prot, int flags, int fd, _off64_t off)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-07-21 21:06:49 +08:00
|
|
|
syscall_printf ("addr %x, len %u, prot %x, flags %x, fd %d, off %D",
|
2000-02-18 03:38:33 +08:00
|
|
|
addr, len, prot, flags, fd, off);
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
caddr_t ret = (caddr_t) MAP_FAILED;
|
|
|
|
fhandler_base *fh = NULL;
|
2005-12-07 19:16:47 +08:00
|
|
|
list *map_list = NULL;
|
|
|
|
size_t orig_len = 0;
|
|
|
|
caddr_t base = NULL;
|
2005-11-29 06:32:29 +08:00
|
|
|
|
|
|
|
DWORD pagesize = getpagesize ();
|
2001-01-16 03:42:33 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
fh_anonymous.set_io_handle (INVALID_HANDLE_VALUE);
|
|
|
|
fh_anonymous.set_access (GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE);
|
|
|
|
fh_disk_file.set_io_handle (NULL);
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
SetResourceLock (LOCK_MMAP_LIST, READ_LOCK | WRITE_LOCK, "mmap");
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* EINVAL error conditions. Note that the addr%pagesize test is deferred
|
2005-11-29 06:32:29 +08:00
|
|
|
to workaround a serious alignment problem in Windows 98. */
|
|
|
|
if (off % pagesize
|
|
|
|
|| ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)))
|
2005-11-30 00:28:05 +08:00
|
|
|
|| ((flags & MAP_TYPE) != MAP_SHARED
|
|
|
|
&& (flags & MAP_TYPE) != MAP_PRIVATE)
|
2005-11-29 06:32:29 +08:00
|
|
|
#if 0
|
2005-12-07 19:16:47 +08:00
|
|
|
|| (fixed (flags) && ((uintptr_t) addr % pagesize))
|
2005-11-29 06:32:29 +08:00
|
|
|
#endif
|
2001-01-16 03:42:33 +08:00
|
|
|
|| !len)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
2001-01-16 03:42:33 +08:00
|
|
|
}
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* There's a serious alignment problem in Windows 98. MapViewOfFile
|
|
|
|
sometimes returns addresses which are page aligned instead of
|
|
|
|
granularity aligned. OTOH, it's not possible to force such an
|
|
|
|
address using MapViewOfFileEx. So what we do here to let it work
|
|
|
|
at least most of the time is, allow 4K aligned addresses in 98,
|
|
|
|
to enable remapping of formerly mapped pages. If no matching
|
|
|
|
free pages exist, check addr again, this time for the real alignment. */
|
|
|
|
DWORD checkpagesize = wincap.has_mmap_alignment_bug () ?
|
2005-12-22 13:57:54 +08:00
|
|
|
getsystempagesize () : pagesize;
|
2005-12-07 19:16:47 +08:00
|
|
|
if (fixed (flags) && ((uintptr_t) addr % checkpagesize))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
goto out;
|
2001-01-16 03:42:33 +08:00
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
if (!anonymous (flags) && fd != -1)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Ensure that fd is open */
|
2001-10-16 07:39:33 +08:00
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
|
|
|
|
2001-10-16 22:04:26 +08:00
|
|
|
fh = cfd;
|
2005-12-07 19:16:47 +08:00
|
|
|
|
|
|
|
/* mmap /dev/zero is like MAP_ANONYMOUS. */
|
2005-03-01 19:51:29 +08:00
|
|
|
if (fh->get_device () == FH_ZERO)
|
2005-12-07 19:16:47 +08:00
|
|
|
flags |= MAP_ANONYMOUS;
|
2001-01-16 06:18:14 +08:00
|
|
|
}
|
2005-12-07 19:16:47 +08:00
|
|
|
if (anonymous (flags) || fd == -1)
|
2001-01-16 06:18:14 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
fh = &fh_anonymous;
|
|
|
|
fd = -1;
|
2005-11-29 06:32:29 +08:00
|
|
|
flags |= MAP_ANONYMOUS;
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Anonymous mappings are always forced to pagesize length with
|
2005-12-22 13:57:54 +08:00
|
|
|
no offset. */
|
2005-12-07 19:16:47 +08:00
|
|
|
len = roundup2 (len, pagesize);
|
2005-12-02 04:35:13 +08:00
|
|
|
off = 0;
|
2005-03-01 19:51:29 +08:00
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
else if (fh->get_device () == FH_FS)
|
2005-03-01 19:51:29 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
/* EACCES error conditions according to SUSv3. File must be opened
|
2005-12-22 13:57:54 +08:00
|
|
|
for reading, regardless of the requested protection, and file must
|
2005-12-07 19:16:47 +08:00
|
|
|
be opened for writing when PROT_WRITE together with MAP_SHARED
|
|
|
|
is requested. */
|
|
|
|
if (!(fh->get_access () & GENERIC_READ)
|
|
|
|
|| (!(fh->get_access () & GENERIC_WRITE)
|
|
|
|
&& (prot & PROT_WRITE) && !priv (flags)))
|
|
|
|
{
|
|
|
|
set_errno (EACCES);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On 9x you can't create mappings with PAGE_WRITECOPY protection if
|
2005-12-22 13:57:54 +08:00
|
|
|
the file isn't explicitely opened with WRITE access. */
|
2005-12-07 19:16:47 +08:00
|
|
|
if (!wincap.is_winnt () && priv (flags)
|
2005-12-22 13:57:54 +08:00
|
|
|
&& !(fh->get_access () & GENERIC_WRITE))
|
2005-12-07 19:16:47 +08:00
|
|
|
{
|
|
|
|
HANDLE h = CreateFile (fh->get_win32_name (),
|
|
|
|
fh->get_access () | GENERIC_WRITE,
|
|
|
|
wincap.shared (), &sec_none_nih,
|
|
|
|
OPEN_EXISTING, 0, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
set_errno (EACCES);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
fh_disk_file.set_io_handle (h);
|
|
|
|
fh_disk_file.set_access (fh->get_access () | GENERIC_WRITE);
|
|
|
|
path_conv pc;
|
|
|
|
pc.set_name (fh->get_win32_name (), "");
|
|
|
|
fh_disk_file.set_name (pc);
|
|
|
|
fh = &fh_disk_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On NT you can't create mappings with PAGE_EXECUTE protection if
|
2005-12-22 13:57:54 +08:00
|
|
|
the file isn't explicitely opened with EXECUTE access. */
|
2005-12-07 19:16:47 +08:00
|
|
|
if (wincap.is_winnt ())
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
HANDLE h = CreateFile (fh->get_win32_name (),
|
|
|
|
fh->get_access () | GENERIC_EXECUTE,
|
|
|
|
wincap.shared (), &sec_none_nih,
|
|
|
|
OPEN_EXISTING, 0, NULL);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
fh_disk_file.set_io_handle (h);
|
|
|
|
fh_disk_file.set_access (fh->get_access () | GENERIC_EXECUTE);
|
|
|
|
fh = &fh_disk_file;
|
|
|
|
}
|
|
|
|
else if (prot & PROT_EXEC)
|
|
|
|
{
|
|
|
|
/* TODO: To be or not to be... I'm opting for refusing this
|
|
|
|
mmap request rather than faking it, but that might break
|
|
|
|
some non-portable code. */
|
|
|
|
set_errno (EACCES);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-01 19:51:29 +08:00
|
|
|
DWORD high;
|
|
|
|
DWORD low = GetFileSize (fh->get_handle (), &high);
|
|
|
|
_off64_t fsiz = ((_off64_t)high << 32) + low;
|
2005-03-08 17:18:47 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Don't allow file mappings beginning beyond EOF since Windows can't
|
2005-03-08 17:18:47 +08:00
|
|
|
handle that POSIX like, unless MAP_AUTOGROW flag is set, which
|
2005-12-07 19:16:47 +08:00
|
|
|
mimics Windows behaviour. */
|
2005-11-30 00:28:05 +08:00
|
|
|
if (off >= fsiz && !autogrow (flags))
|
2005-03-01 19:51:29 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Instead, it seems suitable to return an anonymous mapping of
|
|
|
|
the given size instead. Mapped addresses beyond EOF aren't
|
|
|
|
written back to the file anyway, so the handling is identical
|
|
|
|
to other pages beyond EOF. */
|
|
|
|
fh = &fh_anonymous;
|
|
|
|
len = roundup2 (len, pagesize);
|
|
|
|
prot = PROT_READ | PROT_WRITE | __PROT_ATTACH;
|
|
|
|
flags &= MAP_FIXED;
|
|
|
|
flags |= MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
|
|
|
|
fd = -1;
|
|
|
|
off = 0;
|
|
|
|
goto go_ahead;
|
2005-03-01 19:51:29 +08:00
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
fsiz -= off;
|
2005-12-07 19:16:47 +08:00
|
|
|
/* On NT systems we're creating the pages beyond EOF as reserved,
|
|
|
|
anonymous pages. That's not possible on 9x for two reasons.
|
|
|
|
It neither allows to create reserved pages in the shared memory
|
|
|
|
area, nor does it allow to create page aligend mappings (in
|
2006-01-11 02:11:32 +08:00
|
|
|
contrast to granularity aligned mappings).
|
|
|
|
|
|
|
|
Note that this isn't done in WOW64 environments since apparently
|
|
|
|
WOW64 does not support the AT_ROUND_TO_PAGE flag which is required
|
|
|
|
to get this right. Too bad. */
|
2005-12-07 19:16:47 +08:00
|
|
|
if (wincap.virtual_protect_works_on_shared_pages ()
|
2006-01-11 02:11:32 +08:00
|
|
|
&& !wincap.is_wow64 ()
|
2005-12-07 19:16:47 +08:00
|
|
|
&& ((len > fsiz && !autogrow (flags))
|
|
|
|
|| len < pagesize))
|
|
|
|
orig_len = len;
|
2005-11-29 06:32:29 +08:00
|
|
|
if (len > fsiz)
|
2005-05-02 11:50:11 +08:00
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
if (autogrow (flags))
|
2005-03-08 17:18:47 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Allow mapping beyond EOF if MAP_AUTOGROW flag is set.
|
2005-12-22 13:57:54 +08:00
|
|
|
Check if file has been opened for writing, otherwise
|
2005-12-07 19:16:47 +08:00
|
|
|
MAP_AUTOGROW is invalid. */
|
2005-03-08 17:18:47 +08:00
|
|
|
if (!(fh->get_access () & GENERIC_WRITE))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
2005-03-08 17:18:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Otherwise, don't map beyond EOF, since Windows would change
|
|
|
|
the file to the new length, in contrast to POSIX. */
|
2005-11-29 06:32:29 +08:00
|
|
|
len = fsiz;
|
2005-03-08 17:18:47 +08:00
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
|
|
|
|
/* If the requested offset + len is <= file size, drop MAP_AUTOGROW.
|
2005-05-02 11:50:11 +08:00
|
|
|
This simplifes fhandler::mmap's job. */
|
2005-11-30 00:28:05 +08:00
|
|
|
if (autogrow (flags) && (off + len) <= fsiz)
|
2005-05-02 11:50:11 +08:00
|
|
|
flags &= ~MAP_AUTOGROW;
|
2005-03-01 19:51:29 +08:00
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
go_ahead:
|
|
|
|
|
|
|
|
map_list = mmapped_areas.get_list_by_fd (fd);
|
2001-02-09 05:02:36 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Test if an existing anonymous mapping can be recycled. */
|
2005-11-30 00:28:05 +08:00
|
|
|
if (map_list && anonymous (flags))
|
2005-02-25 19:15:22 +08:00
|
|
|
{
|
2005-11-30 23:05:48 +08:00
|
|
|
caddr_t tried = map_list->try_map (addr, len, flags, off);
|
|
|
|
/* try_map returns NULL if no map matched, otherwise it returns
|
2005-12-22 13:57:54 +08:00
|
|
|
a valid address, of MAP_FAILED in case of a fatal error. */
|
2005-11-30 23:05:48 +08:00
|
|
|
if (tried)
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-11-30 23:05:48 +08:00
|
|
|
ret = tried;
|
|
|
|
goto out;
|
2005-02-25 19:15:22 +08:00
|
|
|
}
|
|
|
|
}
|
2001-09-28 06:39:50 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Deferred alignment test, see above. */
|
|
|
|
if (wincap.has_mmap_alignment_bug ()
|
2005-12-07 19:16:47 +08:00
|
|
|
&& fixed (flags) && ((uintptr_t) addr % pagesize))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
goto out;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
base = mmap_worker (fh, (caddr_t) addr, len, prot, flags, fd, off);
|
|
|
|
if (!base)
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
if (orig_len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
/* If the requested length is bigger than the file size, the
|
|
|
|
remainder is created as anonymous mapping. Actually two
|
|
|
|
mappings are created, first the reminder from the file end to
|
|
|
|
the next 64K boundary as accessible pages with the same
|
|
|
|
protection as the file's pages, then as much pages as necessary
|
|
|
|
to accomodate the requested length, but as reserved pages which
|
|
|
|
raise a SIGBUS when trying to access them. AT_ROUND_TO_PAGE
|
2006-01-11 02:11:32 +08:00
|
|
|
and page protection on shared pages is only supported by 32 bit NT,
|
|
|
|
so don't even try on 9x and in WOW64. This is accomplished by not
|
|
|
|
setting orig_len on 9x and in WOW64 above. */
|
2005-12-07 19:16:47 +08:00
|
|
|
orig_len = roundup2 (orig_len, pagesize);
|
|
|
|
len = roundup2 (len, getsystempagesize ());
|
|
|
|
if (orig_len - len)
|
|
|
|
{
|
|
|
|
orig_len -= len;
|
|
|
|
size_t valid_page_len = orig_len % pagesize;
|
|
|
|
size_t sigbus_page_len = orig_len - valid_page_len;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
caddr_t at_base = base + len;
|
|
|
|
if (valid_page_len)
|
|
|
|
{
|
|
|
|
prot |= __PROT_FILLER;
|
|
|
|
flags &= MAP_SHARED | MAP_PRIVATE;
|
|
|
|
flags |= MAP_ANONYMOUS | MAP_FIXED;
|
|
|
|
at_base = mmap_worker (&fh_anonymous, at_base, valid_page_len,
|
|
|
|
prot, flags, -1, 0);
|
|
|
|
if (!at_base)
|
|
|
|
{
|
|
|
|
fh->munmap (fh->get_handle (), base, len);
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
at_base += valid_page_len;
|
|
|
|
}
|
|
|
|
if (sigbus_page_len)
|
|
|
|
{
|
|
|
|
prot = PROT_READ | PROT_WRITE | __PROT_ATTACH;
|
|
|
|
flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED;
|
|
|
|
at_base = mmap_worker (&fh_anonymous, at_base, sigbus_page_len,
|
|
|
|
prot, flags, -1, 0);
|
|
|
|
if (!at_base)
|
|
|
|
debug_printf ("Warning: Mapping beyond EOF failed, %E");
|
|
|
|
}
|
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
}
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
ret = base;
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
ReleaseResourceLock (LOCK_MMAP_LIST, READ_LOCK | WRITE_LOCK, "mmap");
|
2005-12-07 19:16:47 +08:00
|
|
|
|
|
|
|
if (fh_disk_file.get_handle ())
|
|
|
|
CloseHandle (fh_disk_file.get_handle ());
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
syscall_printf ("%p = mmap() ", ret);
|
2001-01-16 03:42:33 +08:00
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-09 17:41:18 +08:00
|
|
|
extern "C" void *
|
|
|
|
mmap (void *addr, size_t len, int prot, int flags, int fd, _off_t off)
|
2002-02-26 01:47:51 +08:00
|
|
|
{
|
2003-04-02 00:11:41 +08:00
|
|
|
return mmap64 (addr, len, prot, flags, fd, (_off64_t)off);
|
2002-02-26 01:47:51 +08:00
|
|
|
}
|
|
|
|
|
2003-07-06 02:59:35 +08:00
|
|
|
/* munmap () removes all mmapped pages between addr and addr+len. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-03-10 04:10:25 +08:00
|
|
|
extern "C" int
|
2003-09-09 17:41:18 +08:00
|
|
|
munmap (void *addr, size_t len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-07-21 21:06:49 +08:00
|
|
|
syscall_printf ("munmap (addr %x, len %u)", addr, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-07-06 02:59:35 +08:00
|
|
|
/* Error conditions according to SUSv3 */
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!addr || !len || check_invalid_virtual_addr (addr, len))
|
2001-01-16 03:42:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
/* See comment in mmap64 for a description. */
|
2005-12-07 19:16:47 +08:00
|
|
|
size_t pagesize = wincap.has_mmap_alignment_bug () ?
|
|
|
|
getsystempagesize () : getpagesize ();
|
|
|
|
if (((uintptr_t) addr % pagesize) || !len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2005-12-07 19:16:47 +08:00
|
|
|
len = roundup2 (len, pagesize);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "munmap");
|
|
|
|
|
2003-07-06 02:59:35 +08:00
|
|
|
/* Iterate through the map, unmap pages between addr and addr+len
|
|
|
|
in all maps. */
|
2003-09-04 18:27:51 +08:00
|
|
|
list *map_list;
|
2005-11-29 06:32:29 +08:00
|
|
|
for (unsigned list_idx = 0;
|
|
|
|
(map_list = mmapped_areas.get_list (list_idx));
|
2003-09-04 18:27:51 +08:00
|
|
|
++list_idx)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
long record_idx = -1;
|
|
|
|
caddr_t u_addr;
|
|
|
|
DWORD u_len;
|
2003-07-06 02:59:35 +08:00
|
|
|
|
2003-09-09 17:41:18 +08:00
|
|
|
while ((record_idx = map_list->search_record((caddr_t)addr, len, u_addr,
|
2003-11-29 04:55:59 +08:00
|
|
|
u_len, record_idx)) >= 0)
|
2003-09-04 18:27:51 +08:00
|
|
|
{
|
|
|
|
mmap_record *rec = map_list->get_record (record_idx);
|
|
|
|
if (rec->unmap_pages (u_addr, u_len))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-26 16:50:12 +08:00
|
|
|
/* The whole record has been unmapped, so we now actually
|
2003-11-29 04:55:59 +08:00
|
|
|
unmap it from the system in full length... */
|
2003-09-04 18:27:51 +08:00
|
|
|
fhandler_base *fh = rec->alloc_fh ();
|
2003-09-26 16:50:12 +08:00
|
|
|
fh->munmap (rec->get_handle (),
|
2003-11-29 04:55:59 +08:00
|
|
|
rec->get_address (),
|
2005-11-29 06:32:29 +08:00
|
|
|
rec->get_len ());
|
2003-09-04 18:27:51 +08:00
|
|
|
rec->free_fh (fh);
|
2000-10-05 21:07:02 +08:00
|
|
|
|
2003-09-26 16:50:12 +08:00
|
|
|
/* ...and delete the record. */
|
2003-09-04 18:27:51 +08:00
|
|
|
if (map_list->del_record (record_idx--))
|
|
|
|
{
|
|
|
|
/* Yay, the last record has been removed from the list,
|
|
|
|
we can remove the list now, too. */
|
2005-11-29 06:32:29 +08:00
|
|
|
mmapped_areas.del_list (list_idx--);
|
2003-09-04 18:27:51 +08:00
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-01-16 03:42:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "munmap");
|
2003-07-06 02:59:35 +08:00
|
|
|
syscall_printf ("0 = munmap(): %x", addr);
|
|
|
|
return 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sync file with memory. Ignore flags for now. */
|
|
|
|
|
2003-03-10 04:10:25 +08:00
|
|
|
extern "C" int
|
2003-09-09 17:41:18 +08:00
|
|
|
msync (void *addr, size_t len, int flags)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
int ret = -1;
|
|
|
|
list *map_list;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
syscall_printf ("msync (addr: %p, len %u, flags %x)", addr, len, flags);
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "msync");
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* See comment in mmap64 for a description. */
|
|
|
|
size_t pagesize = wincap.has_mmap_alignment_bug () ?
|
|
|
|
getsystempagesize () : getpagesize ();
|
|
|
|
if (((uintptr_t) addr % pagesize)
|
|
|
|
|| (flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE))
|
|
|
|
|| (flags & (MS_ASYNC | MS_SYNC) == (MS_ASYNC | MS_SYNC)))
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
2000-10-05 21:07:02 +08:00
|
|
|
}
|
2005-12-07 19:16:47 +08:00
|
|
|
len = roundup2 (len, pagesize);
|
2000-10-05 21:07:02 +08:00
|
|
|
|
|
|
|
/* Iterate through the map, looking for the mmapped area.
|
|
|
|
Error if not found. */
|
2005-11-29 06:32:29 +08:00
|
|
|
for (unsigned list_idx = 0;
|
|
|
|
(map_list = mmapped_areas.get_list (list_idx));
|
2003-09-04 18:27:51 +08:00
|
|
|
++list_idx)
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
mmap_record *rec;
|
|
|
|
for (int record_idx = 0;
|
2003-11-29 04:55:59 +08:00
|
|
|
(rec = map_list->get_record (record_idx));
|
2003-09-04 18:27:51 +08:00
|
|
|
++record_idx)
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
2003-09-09 17:41:18 +08:00
|
|
|
if (rec->access ((caddr_t)addr))
|
2001-02-05 20:36:41 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
/* Check whole area given by len. */
|
|
|
|
for (DWORD i = getpagesize (); i < len; ++i)
|
2003-09-09 17:41:18 +08:00
|
|
|
if (!rec->access ((caddr_t)addr + i))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
goto out;
|
|
|
|
}
|
2003-09-04 18:27:51 +08:00
|
|
|
fhandler_base *fh = rec->alloc_fh ();
|
2005-11-29 06:32:29 +08:00
|
|
|
ret = fh->msync (rec->get_handle (), (caddr_t)addr, len, flags);
|
2003-09-04 18:27:51 +08:00
|
|
|
rec->free_fh (fh);
|
2005-11-29 06:32:29 +08:00
|
|
|
goto out;
|
2002-03-12 04:23:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-10-05 21:07:02 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* No matching mapping exists. */
|
2000-10-05 21:07:02 +08:00
|
|
|
set_errno (ENOMEM);
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
out:
|
|
|
|
syscall_printf ("%d = msync()", ret);
|
2002-09-22 11:38:57 +08:00
|
|
|
ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "msync");
|
2005-11-29 06:32:29 +08:00
|
|
|
return ret;
|
2000-10-05 21:07:02 +08:00
|
|
|
}
|
|
|
|
|
2003-09-09 17:41:18 +08:00
|
|
|
/* Set memory protection */
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mprotect (void *addr, size_t len, int prot)
|
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
bool in_mapped = false;
|
|
|
|
bool ret = false;
|
2003-09-09 17:41:18 +08:00
|
|
|
DWORD old_prot;
|
|
|
|
DWORD new_prot = 0;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
syscall_printf ("mprotect (addr: %p, len %u, prot %x)", addr, len, prot);
|
2003-09-09 17:41:18 +08:00
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* See comment in mmap64 for a description. */
|
|
|
|
size_t pagesize = wincap.has_mmap_alignment_bug () ?
|
|
|
|
getsystempagesize () : getpagesize ();
|
|
|
|
if ((uintptr_t) addr % pagesize)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
len = roundup2 (len, pagesize);
|
2003-09-09 17:41:18 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
SetResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "mprotect");
|
2005-12-06 04:20:18 +08:00
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Iterate through the map, protect pages between addr and addr+len
|
|
|
|
in all maps. */
|
|
|
|
list *map_list;
|
|
|
|
for (unsigned list_idx = 0;
|
|
|
|
(map_list = mmapped_areas.get_list (list_idx));
|
|
|
|
++list_idx)
|
|
|
|
{
|
|
|
|
long record_idx = -1;
|
|
|
|
caddr_t u_addr;
|
|
|
|
DWORD u_len;
|
|
|
|
|
|
|
|
while ((record_idx = map_list->search_record((caddr_t)addr, len,
|
|
|
|
u_addr, u_len,
|
|
|
|
record_idx)) >= 0)
|
|
|
|
{
|
|
|
|
mmap_record *rec = map_list->get_record (record_idx);
|
|
|
|
in_mapped = true;
|
2005-12-07 19:16:47 +08:00
|
|
|
if (rec->attached ())
|
|
|
|
continue;
|
2005-11-29 06:32:29 +08:00
|
|
|
new_prot = gen_protect (prot, rec->get_flags ());
|
2005-11-30 00:28:05 +08:00
|
|
|
if (rec->anonymous () && rec->priv () && rec->noreserve ())
|
|
|
|
{
|
|
|
|
if (new_prot == PAGE_NOACCESS)
|
|
|
|
ret = VirtualFree (u_addr, u_len, MEM_DECOMMIT);
|
|
|
|
else
|
|
|
|
ret = !!VirtualAlloc (u_addr, u_len, MEM_COMMIT, new_prot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = VirtualProtect (u_addr, u_len, new_prot, &old_prot);
|
|
|
|
if (!ret)
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
2005-12-07 19:16:47 +08:00
|
|
|
break;
|
2005-11-29 06:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseResourceLock (LOCK_MMAP_LIST, WRITE_LOCK | READ_LOCK, "mprotect");
|
|
|
|
|
|
|
|
if (!in_mapped)
|
2004-07-16 01:00:44 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
int flags = 0;
|
2005-11-30 00:28:05 +08:00
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
ret = VirtualQuery (addr, &mbi, sizeof mbi);
|
|
|
|
if (ret)
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
/* If write protection is requested, check if the page was
|
|
|
|
originally protected writecopy. In this case call VirtualProtect
|
|
|
|
requesting PAGE_WRITECOPY, otherwise the VirtualProtect will fail
|
|
|
|
on NT version >= 5.0 */
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
{
|
|
|
|
if (mbi.AllocationProtect == PAGE_WRITECOPY
|
|
|
|
|| mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY)
|
|
|
|
flags = MAP_PRIVATE;
|
|
|
|
}
|
|
|
|
new_prot = gen_protect (prot, flags);
|
|
|
|
if (new_prot != PAGE_NOACCESS && mbi.State == MEM_RESERVE)
|
|
|
|
ret = VirtualAlloc (addr, len, MEM_COMMIT, new_prot);
|
|
|
|
else
|
|
|
|
ret = VirtualProtect (addr, len, new_prot, &old_prot);
|
2005-11-29 06:32:29 +08:00
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!ret)
|
2005-12-07 19:16:47 +08:00
|
|
|
__seterrno ();
|
2003-09-09 17:41:18 +08:00
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
out:
|
|
|
|
|
|
|
|
syscall_printf ("%d = mprotect ()", ret ? 0 : -1);
|
|
|
|
return ret ? 0 : -1;
|
2003-09-09 17:41:18 +08:00
|
|
|
}
|
|
|
|
|
2005-10-19 02:51:33 +08:00
|
|
|
extern "C" int
|
|
|
|
mlock (const void *addr, size_t len)
|
|
|
|
{
|
|
|
|
if (!wincap.has_working_virtual_lock ())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* Instead of using VirtualLock, which does not guarantee that the pages
|
|
|
|
aren't swapped out when the process is inactive, we're using
|
|
|
|
ZwLockVirtualMemory with the LOCK_VM_IN_RAM flag to do what mlock on
|
|
|
|
POSIX systems does. On NT, this requires SeLockMemoryPrivilege,
|
|
|
|
which is given only to SYSTEM by default. */
|
|
|
|
|
|
|
|
push_thread_privilege (SE_LOCK_MEMORY_PRIV, true);
|
|
|
|
|
|
|
|
/* Align address and length values to page size. */
|
2005-12-07 19:16:47 +08:00
|
|
|
size_t pagesize = getpagesize ();
|
|
|
|
PVOID base = (PVOID) rounddown((uintptr_t) addr, pagesize);
|
|
|
|
ULONG size = roundup2 (((uintptr_t) addr - (uintptr_t) base) + len, pagesize);
|
2005-10-19 02:51:33 +08:00
|
|
|
NTSTATUS status = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = NtLockVirtualMemory (hMainProc, &base, &size, LOCK_VM_IN_RAM);
|
|
|
|
if (status == STATUS_WORKING_SET_QUOTA)
|
|
|
|
{
|
|
|
|
/* The working set is too small, try to increase it so that the
|
|
|
|
requested locking region fits in. Unfortunately I don't know
|
|
|
|
any function which would return the currently locked pages of
|
|
|
|
a process (no go with NtQueryVirtualMemory).
|
2005-12-22 13:57:54 +08:00
|
|
|
|
2005-10-19 02:51:33 +08:00
|
|
|
So, except for the border cases, what we do here is something
|
|
|
|
really embarrassing. We raise the working set by 64K at a time
|
|
|
|
and retry, until either we fail to raise the working set size
|
|
|
|
further, or until NtLockVirtualMemory returns successfully (or
|
|
|
|
with another error). */
|
|
|
|
ULONG min, max;
|
|
|
|
if (!GetProcessWorkingSetSize (hMainProc, &min, &max))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (min < size)
|
2005-11-29 06:32:29 +08:00
|
|
|
min = size + pagesize;
|
|
|
|
else if (size < pagesize)
|
2005-10-19 02:51:33 +08:00
|
|
|
min += size;
|
|
|
|
else
|
2005-11-29 06:32:29 +08:00
|
|
|
min += pagesize;
|
2005-10-19 02:51:33 +08:00
|
|
|
if (max < min)
|
|
|
|
max = min;
|
|
|
|
if (!SetProcessWorkingSetSize (hMainProc, min, max))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
else
|
2005-12-22 13:57:54 +08:00
|
|
|
ret = 0;
|
2005-10-19 02:51:33 +08:00
|
|
|
}
|
|
|
|
while (status == STATUS_WORKING_SET_QUOTA);
|
|
|
|
|
|
|
|
pop_thread_privilege ();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
munlock (const void *addr, size_t len)
|
|
|
|
{
|
|
|
|
if (!wincap.has_working_virtual_lock ())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
push_thread_privilege (SE_LOCK_MEMORY_PRIV, true);
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Align address and length values to page size. */
|
|
|
|
size_t pagesize = getpagesize ();
|
|
|
|
PVOID base = (PVOID) rounddown((uintptr_t) addr, pagesize);
|
|
|
|
ULONG size = roundup2 (((uintptr_t) addr - (uintptr_t) base) + len, pagesize);
|
2005-10-19 02:51:33 +08:00
|
|
|
NTSTATUS status = NtUnlockVirtualMemory (hMainProc, &base, &size,
|
|
|
|
LOCK_VM_IN_RAM);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
pop_thread_privilege ();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-10-05 21:07:02 +08:00
|
|
|
/*
|
|
|
|
* Base implementation:
|
|
|
|
*
|
|
|
|
* `mmap' returns ENODEV as documented in SUSv2.
|
|
|
|
* In contrast to the global function implementation, the member function
|
|
|
|
* `mmap' has to return the mapped base address in `addr' and the handle to
|
|
|
|
* the mapping object as return value. In case of failure, the fhandler
|
|
|
|
* mmap has to close that handle by itself and return INVALID_HANDLE_VALUE.
|
|
|
|
*
|
|
|
|
* `munmap' and `msync' get the handle to the mapping object as first parameter
|
|
|
|
* additionally.
|
|
|
|
*/
|
|
|
|
HANDLE
|
2005-11-29 06:32:29 +08:00
|
|
|
fhandler_base::mmap (caddr_t *addr, size_t len, int prot,
|
2003-04-02 00:11:41 +08:00
|
|
|
int flags, _off64_t off)
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
|
|
|
set_errno (ENODEV);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::munmap (HANDLE h, caddr_t addr, size_t len)
|
|
|
|
{
|
|
|
|
set_errno (ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_base::msync (HANDLE h, caddr_t addr, size_t len, int flags)
|
|
|
|
{
|
|
|
|
set_errno (ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
bool
|
2005-11-29 06:32:29 +08:00
|
|
|
fhandler_base::fixup_mmap_after_fork (HANDLE h, int prot, int flags,
|
2005-02-26 05:23:15 +08:00
|
|
|
_off64_t offset, DWORD size,
|
|
|
|
void *address)
|
2001-01-11 20:33:12 +08:00
|
|
|
{
|
|
|
|
set_errno (ENODEV);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-30 00:28:05 +08:00
|
|
|
/* Implementation for anonymous maps. Using fhandler_dev_zero looks
|
|
|
|
quite the natural way. */
|
|
|
|
HANDLE
|
|
|
|
fhandler_dev_zero::mmap (caddr_t *addr, size_t len, int prot,
|
|
|
|
int flags, _off64_t off)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
void *base;
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
if (priv (flags) && !filler (prot))
|
2005-11-30 00:28:05 +08:00
|
|
|
{
|
|
|
|
/* Private anonymous maps are now implemented using VirtualAlloc.
|
2005-12-22 13:57:54 +08:00
|
|
|
This has two advantages:
|
2005-11-30 00:28:05 +08:00
|
|
|
|
|
|
|
- VirtualAlloc has a smaller footprint than a copy-on-write
|
|
|
|
anonymous map.
|
|
|
|
|
|
|
|
- It supports decommitting using VirtualFree, in contrast to
|
|
|
|
section maps. This allows minimum footprint private maps,
|
|
|
|
when using the (non-POSIX, yay-Linux) MAP_NORESERVE flag.
|
|
|
|
*/
|
|
|
|
DWORD protect = gen_protect (prot, flags);
|
|
|
|
DWORD alloc_type = MEM_RESERVE | (noreserve (flags) ? 0 : MEM_COMMIT);
|
|
|
|
base = VirtualAlloc (*addr, len, alloc_type, protect);
|
|
|
|
if (!base && addr && !fixed (flags))
|
|
|
|
base = VirtualAlloc (NULL, len, alloc_type, protect);
|
|
|
|
if (!base || (fixed (flags) && base != *addr))
|
|
|
|
{
|
|
|
|
if (!base)
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
{
|
2005-12-19 23:25:23 +08:00
|
|
|
VirtualFree (base, 0, MEM_RELEASE);
|
2005-11-30 00:28:05 +08:00
|
|
|
set_errno (EINVAL);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("VirtualAlloc: address shift with MAP_FIXED given");
|
2005-11-30 00:28:05 +08:00
|
|
|
}
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
h = (HANDLE) 1; /* Fake handle to indicate success. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
h = mmap_func->CreateMapping (get_handle (), len, off, get_access (),
|
|
|
|
prot, flags, get_win32_name ());
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!h)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("CreateMapping failed with %E");
|
2005-11-30 00:28:05 +08:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
base = mmap_func->MapView (h, *addr, len, get_access(), prot, flags, off);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!base || (fixed (flags) && base != *addr))
|
|
|
|
{
|
|
|
|
if (!base)
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (base);
|
|
|
|
set_errno (EINVAL);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("MapView: address shift with MAP_FIXED given");
|
2005-11-30 00:28:05 +08:00
|
|
|
}
|
|
|
|
CloseHandle (h);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*addr = (caddr_t) base;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_zero::munmap (HANDLE h, caddr_t addr, size_t len)
|
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
if (h == (HANDLE) 1) /* See fhandler_dev_zero::mmap. */
|
2005-12-19 23:25:23 +08:00
|
|
|
VirtualFree (addr, 0, MEM_RELEASE);
|
2005-12-07 19:16:47 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (addr);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
2005-11-30 00:28:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_zero::msync (HANDLE h, caddr_t addr, size_t len, int flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_zero::fixup_mmap_after_fork (HANDLE h, int prot, int flags,
|
|
|
|
_off64_t offset, DWORD size,
|
|
|
|
void *address)
|
|
|
|
{
|
|
|
|
/* Re-create the map */
|
|
|
|
void *base;
|
2005-12-07 19:16:47 +08:00
|
|
|
if (priv (flags) && !filler (prot))
|
2005-11-30 00:28:05 +08:00
|
|
|
{
|
|
|
|
DWORD alloc_type = MEM_RESERVE | (noreserve (flags) ? 0 : MEM_COMMIT);
|
2005-11-30 23:05:48 +08:00
|
|
|
/* Always allocate R/W so that ReadProcessMemory doesn't fail
|
2005-12-22 13:57:54 +08:00
|
|
|
due to a non-writable target address. The protection is
|
2005-11-30 23:05:48 +08:00
|
|
|
set to the correct one anyway in the fixup loop. */
|
|
|
|
base = VirtualAlloc (address, size, alloc_type, PAGE_READWRITE);
|
2005-11-30 00:28:05 +08:00
|
|
|
}
|
|
|
|
else
|
2005-12-07 19:16:47 +08:00
|
|
|
base = mmap_func->MapView (h, address, size, get_access (),
|
|
|
|
prot, flags, offset);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (base != address)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
VirtualQuery (address, &m, sizeof (m));
|
2005-12-15 17:04:28 +08:00
|
|
|
system_printf ("requested %p != %p mem alloc base %p, state %p, "
|
|
|
|
"size %d, %E", address, base, m.AllocationBase, m.State,
|
|
|
|
m.RegionSize);
|
2005-11-30 00:28:05 +08:00
|
|
|
}
|
|
|
|
return base == address;
|
|
|
|
}
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Implementation for disk files and anonymous mappings. */
|
2000-10-05 21:07:02 +08:00
|
|
|
HANDLE
|
2005-11-29 06:32:29 +08:00
|
|
|
fhandler_disk_file::mmap (caddr_t *addr, size_t len, int prot,
|
2003-04-02 00:11:41 +08:00
|
|
|
int flags, _off64_t off)
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
HANDLE h = mmap_func->CreateMapping (get_handle (), len, off, get_access (),
|
|
|
|
prot, flags, get_win32_name ());
|
2001-07-11 05:04:14 +08:00
|
|
|
if (!h)
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("CreateMapping failed with %E");
|
2000-10-05 21:07:02 +08:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
void *base = mmap_func->MapView (h, *addr, len, get_access (),
|
2005-12-22 13:57:54 +08:00
|
|
|
prot, flags, off);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!base || (fixed (flags) && base != *addr))
|
2000-10-05 21:07:02 +08:00
|
|
|
{
|
2000-10-08 01:35:36 +08:00
|
|
|
if (!base)
|
2005-11-29 06:32:29 +08:00
|
|
|
__seterrno ();
|
2000-10-08 01:35:36 +08:00
|
|
|
else
|
2001-09-03 10:13:05 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
UnmapViewOfFile (base);
|
2000-10-08 01:35:36 +08:00
|
|
|
set_errno (EINVAL);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("MapView: address shift with MAP_FIXED given");
|
2001-09-03 10:13:05 +08:00
|
|
|
}
|
2000-10-05 21:07:02 +08:00
|
|
|
CloseHandle (h);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = (caddr_t) base;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_disk_file::munmap (HANDLE h, caddr_t addr, size_t len)
|
|
|
|
{
|
|
|
|
UnmapViewOfFile (addr);
|
|
|
|
CloseHandle (h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_disk_file::msync (HANDLE h, caddr_t addr, size_t len, int flags)
|
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
if (FlushViewOfFile (addr, len) == 0)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
bool
|
2005-11-29 06:32:29 +08:00
|
|
|
fhandler_disk_file::fixup_mmap_after_fork (HANDLE h, int prot, int flags,
|
2005-02-26 05:23:15 +08:00
|
|
|
_off64_t offset, DWORD size,
|
|
|
|
void *address)
|
2001-01-11 20:33:12 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Re-create the map */
|
2005-12-07 19:16:47 +08:00
|
|
|
void *base = mmap_func->MapView (h, address, size, get_access (),
|
|
|
|
prot, flags, offset);
|
2005-11-29 06:32:29 +08:00
|
|
|
if (base != address)
|
2005-02-26 05:23:15 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
VirtualQuery (address, &m, sizeof (m));
|
|
|
|
system_printf ("requested %p != %p mem alloc base %p, state %p, "
|
|
|
|
"size %d, %E", address, base, m.AllocationBase, m.State,
|
|
|
|
m.RegionSize);
|
2005-02-26 05:23:15 +08:00
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
return base == address;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_dev_mem::mmap (caddr_t *addr, size_t len, int prot,
|
|
|
|
int flags, _off64_t off)
|
|
|
|
{
|
|
|
|
if (off >= mem_size
|
|
|
|
|| (DWORD) len >= mem_size
|
|
|
|
|| off + len >= mem_size)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("-1 = mmap(): illegal parameter, set EINVAL");
|
2005-11-29 06:32:29 +08:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNICODE_STRING memstr;
|
|
|
|
RtlInitUnicodeString (&memstr, L"\\device\\physicalmemory");
|
|
|
|
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
InitializeObjectAttributes (&attr, &memstr,
|
|
|
|
OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
/* Section access is bit-wise ored, while on the Win32 level access
|
|
|
|
is only one of the values. It's not quite clear if the section
|
|
|
|
access has to be defined this way, or if SECTION_ALL_ACCESS would
|
|
|
|
be sufficient but this worked fine so far, so why change? */
|
|
|
|
ACCESS_MASK section_access;
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
section_access = SECTION_MAP_READ | SECTION_MAP_WRITE;
|
2005-02-26 05:23:15 +08:00
|
|
|
else
|
2005-11-29 06:32:29 +08:00
|
|
|
section_access = SECTION_MAP_READ;
|
|
|
|
|
|
|
|
HANDLE h;
|
|
|
|
NTSTATUS ret = NtOpenSection (&h, section_access, &attr);
|
|
|
|
if (!NT_SUCCESS (ret))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (ret);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("-1 = mmap(): NtOpenSection failed with %E");
|
2005-11-29 06:32:29 +08:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2005-12-07 19:16:47 +08:00
|
|
|
void *base = MapViewNT (h, *addr, len, get_access (),
|
2005-12-22 13:57:54 +08:00
|
|
|
prot, flags | MAP_ANONYMOUS, off);
|
2005-11-30 00:28:05 +08:00
|
|
|
if (!base || (fixed (flags) && base != *addr))
|
2005-11-29 06:32:29 +08:00
|
|
|
{
|
|
|
|
if (!base)
|
2005-12-22 13:57:54 +08:00
|
|
|
__seterrno ();
|
2005-11-29 06:32:29 +08:00
|
|
|
else
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
NtUnmapViewOfSection (GetCurrentProcess (), base);
|
|
|
|
set_errno (EINVAL);
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("MapView: address shift with MAP_FIXED given");
|
2005-11-29 06:32:29 +08:00
|
|
|
}
|
|
|
|
CloseHandle (h);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = (caddr_t) base;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_mem::munmap (HANDLE h, caddr_t addr, size_t len)
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
if (!NT_SUCCESS (ret = NtUnmapViewOfSection (INVALID_HANDLE_VALUE, addr)))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle (h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev_mem::msync (HANDLE h, caddr_t addr, size_t len, int flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_dev_mem::fixup_mmap_after_fork (HANDLE h, int prot, int flags,
|
|
|
|
_off64_t offset, DWORD size,
|
|
|
|
void *address)
|
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
void *base = MapViewNT (h, address, size, get_access (), prot,
|
2005-12-02 04:35:13 +08:00
|
|
|
flags | MAP_ANONYMOUS, offset);
|
2002-10-21 09:00:58 +08:00
|
|
|
if (base != address)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
2005-07-07 04:05:03 +08:00
|
|
|
VirtualQuery (address, &m, sizeof (m));
|
2005-11-29 06:32:29 +08:00
|
|
|
system_printf ("requested %p != %p mem alloc base %p, state %p, "
|
|
|
|
"size %d, %E", address, base, m.AllocationBase, m.State,
|
|
|
|
m.RegionSize);
|
2002-10-21 09:00:58 +08:00
|
|
|
}
|
2001-01-11 20:33:12 +08:00
|
|
|
return base == address;
|
|
|
|
}
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Call to re-create all the file mappings in a forked child. Called from
|
|
|
|
the child in initialization. At this point we are passed a valid
|
|
|
|
mmapped_areas map, and all the HANDLE's are valid for the child, but
|
|
|
|
none of the mapped areas are in our address space. We need to iterate
|
|
|
|
through the map, doing the MapViewOfFile calls. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int __stdcall
|
2002-03-12 01:57:22 +08:00
|
|
|
fixup_mmaps_after_fork (HANDLE parent)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Iterate through the map */
|
2003-09-04 18:27:51 +08:00
|
|
|
list *map_list;
|
2005-11-29 06:32:29 +08:00
|
|
|
for (unsigned list_idx = 0;
|
|
|
|
(map_list = mmapped_areas.get_list (list_idx));
|
2003-09-04 18:27:51 +08:00
|
|
|
++list_idx)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-04 18:27:51 +08:00
|
|
|
mmap_record *rec;
|
|
|
|
for (int record_idx = 0;
|
2003-11-29 04:55:59 +08:00
|
|
|
(rec = map_list->get_record (record_idx));
|
2003-09-04 18:27:51 +08:00
|
|
|
++record_idx)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-12-02 04:35:13 +08:00
|
|
|
debug_printf ("fd %d, h 0x%x, address %p, len 0x%x, prot: 0x%x, "
|
|
|
|
"flags: 0x%x, offset %X",
|
|
|
|
rec->get_fd (), rec->get_handle (), rec->get_address (),
|
|
|
|
rec->get_len (), rec->get_prot (), rec->get_flags (),
|
|
|
|
rec->get_offset ());
|
2003-09-04 18:27:51 +08:00
|
|
|
|
|
|
|
fhandler_base *fh = rec->alloc_fh ();
|
2003-12-08 06:37:12 +08:00
|
|
|
bool ret = fh->fixup_mmap_after_fork (rec->get_handle (),
|
2005-11-29 06:32:29 +08:00
|
|
|
rec->get_prot (),
|
2005-12-02 04:35:13 +08:00
|
|
|
rec->get_flags () | MAP_FIXED,
|
2003-09-04 18:27:51 +08:00
|
|
|
rec->get_offset (),
|
2005-11-29 06:32:29 +08:00
|
|
|
rec->get_len (),
|
2003-09-04 18:27:51 +08:00
|
|
|
rec->get_address ());
|
|
|
|
rec->free_fh (fh);
|
|
|
|
|
|
|
|
if (!ret)
|
2005-12-07 19:16:47 +08:00
|
|
|
{
|
|
|
|
if (rec->attached ())
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-12-07 19:16:47 +08:00
|
|
|
system_printf ("Warning: Fixup mapping beyond EOF failed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
DWORD old_prot;
|
|
|
|
|
|
|
|
for (char *address = rec->get_address ();
|
|
|
|
address < rec->get_address () + rec->get_len ();
|
|
|
|
address += mbi.RegionSize)
|
2003-09-04 18:27:51 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!VirtualQueryEx (parent, address, &mbi, sizeof mbi))
|
2005-12-22 13:57:54 +08:00
|
|
|
{
|
2005-11-29 06:32:29 +08:00
|
|
|
system_printf ("VirtualQueryEx failed for MAP_PRIVATE "
|
2005-12-22 13:57:54 +08:00
|
|
|
"address %p, %E", address);
|
2005-11-29 06:32:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-12-07 19:16:47 +08:00
|
|
|
/* Just skip reserved pages. */
|
2005-11-29 06:32:29 +08:00
|
|
|
if (mbi.State == MEM_RESERVE)
|
2005-12-07 19:16:47 +08:00
|
|
|
continue;
|
2005-11-29 06:32:29 +08:00
|
|
|
/* Copy-on-write pages must be copied to the child to circumvent
|
2005-12-22 13:57:54 +08:00
|
|
|
a strange notion how copy-on-write is supposed to work. */
|
2005-11-29 06:32:29 +08:00
|
|
|
if (rec->priv ())
|
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
if (rec->anonymous () && rec->noreserve ()
|
|
|
|
&& !VirtualAlloc (address, mbi.RegionSize,
|
2005-12-22 13:57:54 +08:00
|
|
|
MEM_COMMIT, PAGE_READWRITE))
|
2005-11-30 00:28:05 +08:00
|
|
|
{
|
|
|
|
system_printf ("VirtualAlloc failed for MAP_PRIVATE "
|
|
|
|
"address %p, %E", address);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-29 06:32:29 +08:00
|
|
|
if (mbi.Protect == PAGE_NOACCESS
|
|
|
|
&& !VirtualProtectEx (parent, address, mbi.RegionSize,
|
|
|
|
PAGE_READONLY, &old_prot))
|
|
|
|
{
|
|
|
|
system_printf ("VirtualProtectEx failed for MAP_PRIVATE "
|
|
|
|
"address %p, %E", address);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-12-12 18:00:32 +08:00
|
|
|
else if ((mbi.AllocationProtect == PAGE_WRITECOPY
|
|
|
|
|| mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY)
|
2005-11-30 00:28:05 +08:00
|
|
|
&& (mbi.Protect == PAGE_READWRITE
|
|
|
|
|| mbi.Protect == PAGE_EXECUTE_READWRITE))
|
2005-12-12 18:00:32 +08:00
|
|
|
/* A WRITECOPY page which has been written to is set to
|
|
|
|
READWRITE, but that's an incompatible protection to
|
|
|
|
set the page to. Convert the protection to WRITECOPY
|
|
|
|
so that the below VirtualProtect doesn't fail. */
|
|
|
|
mbi.Protect <<= 1;
|
|
|
|
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!ReadProcessMemory (parent, address, address,
|
|
|
|
mbi.RegionSize, NULL))
|
|
|
|
{
|
|
|
|
system_printf ("ReadProcessMemory failed for MAP_PRIVATE "
|
|
|
|
"address %p, %E", address);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (mbi.Protect == PAGE_NOACCESS
|
|
|
|
&& !VirtualProtectEx (parent, address, mbi.RegionSize,
|
|
|
|
PAGE_NOACCESS, &old_prot))
|
|
|
|
{
|
|
|
|
system_printf ("WARNING: VirtualProtectEx to return to "
|
|
|
|
"PAGE_NOACCESS state in parent failed for "
|
|
|
|
"MAP_PRIVATE address %p, %E", address);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2005-11-30 23:05:48 +08:00
|
|
|
/* Set child page protection to parent protection */
|
2005-11-29 06:32:29 +08:00
|
|
|
if (!VirtualProtect (address, mbi.RegionSize,
|
|
|
|
mbi.Protect, &old_prot))
|
|
|
|
{
|
2005-11-30 00:28:05 +08:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
VirtualQuery (address, &m, sizeof m);
|
2005-11-29 06:32:29 +08:00
|
|
|
system_printf ("VirtualProtect failed for "
|
2005-12-22 13:57:54 +08:00
|
|
|
"address %p, "
|
2005-11-30 00:28:05 +08:00
|
|
|
"parentstate: 0x%x, "
|
|
|
|
"state: 0x%x, "
|
|
|
|
"parentprot: 0x%x, "
|
|
|
|
"prot: 0x%x, %E",
|
|
|
|
address, mbi.State, m.State,
|
|
|
|
mbi.Protect, m.Protect);
|
2005-11-29 06:32:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-11-15 14:27:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
debug_printf ("succeeded");
|
|
|
|
return 0;
|
|
|
|
}
|