2000-09-03 12:16:35 +08:00
|
|
|
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <assert.h>
|
2000-10-02 10:26:04 +08:00
|
|
|
#include <stdlib.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-09-10 00:52:37 +08:00
|
|
|
#include "path.h"
|
2011-04-21 08:53:55 +08:00
|
|
|
#include "tty.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"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "dtable.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2001-07-17 11:41:52 +08:00
|
|
|
#include "child_info.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "heap.h"
|
2003-09-01 10:05:32 +08:00
|
|
|
#include "sigproc.h"
|
2005-06-01 11:46:56 +08:00
|
|
|
#include "pinfo.h"
|
2012-02-14 19:27:43 +08:00
|
|
|
#include "registry.h"
|
|
|
|
#include "ntdll.h"
|
2022-10-27 03:16:35 +08:00
|
|
|
#include "memory_layout.h"
|
2005-06-01 11:46:56 +08:00
|
|
|
#include <unistd.h>
|
2008-02-15 00:47:11 +08:00
|
|
|
#include <wchar.h>
|
2013-04-23 17:44:36 +08:00
|
|
|
#include <sys/param.h>
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2011-05-30 14:24:53 +08:00
|
|
|
static mini_cygheap NO_COPY cygheap_dummy =
|
2009-06-08 11:53:40 +08:00
|
|
|
{
|
2016-07-21 04:05:59 +08:00
|
|
|
{__utf8_mbtowc}
|
2009-06-08 11:53:40 +08:00
|
|
|
};
|
|
|
|
|
2011-05-30 14:24:53 +08:00
|
|
|
init_cygheap NO_COPY *cygheap = (init_cygheap *) &cygheap_dummy;
|
2002-10-15 04:25:52 +08:00
|
|
|
void NO_COPY *cygheap_max;
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2005-04-05 12:31:00 +08:00
|
|
|
static NO_COPY muto cygheap_protect;
|
2000-10-02 10:26:04 +08:00
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
struct cygheap_entry
|
2009-06-08 11:53:40 +08:00
|
|
|
{
|
|
|
|
int type;
|
|
|
|
struct cygheap_entry *next;
|
|
|
|
char data[0];
|
|
|
|
};
|
2001-07-17 11:41:52 +08:00
|
|
|
|
2012-08-10 03:58:53 +08:00
|
|
|
class tls_sentry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static muto lock;
|
|
|
|
int destroy;
|
|
|
|
void init ();
|
|
|
|
bool acquired () {return lock.acquired ();}
|
|
|
|
tls_sentry () {destroy = 0;}
|
|
|
|
tls_sentry (DWORD wait) {destroy = lock.acquire (wait);}
|
|
|
|
~tls_sentry () {if (destroy) lock.release ();}
|
|
|
|
};
|
|
|
|
|
|
|
|
muto NO_COPY tls_sentry::lock;
|
2013-04-23 17:44:36 +08:00
|
|
|
static NO_COPY uint32_t nthreads;
|
2012-08-10 03:58:53 +08:00
|
|
|
|
|
|
|
#define THREADLIST_CHUNK 256
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - offsetof (_cmalloc_entry, data)))
|
2001-07-17 11:41:52 +08:00
|
|
|
|
2001-08-05 05:10:52 +08:00
|
|
|
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
|
|
|
|
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
|
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
extern "C" {
|
2022-05-24 03:52:52 +08:00
|
|
|
static void _cfree (void *);
|
2022-06-07 00:00:45 +08:00
|
|
|
static void *_csbrk (int);
|
2001-08-05 05:10:52 +08:00
|
|
|
}
|
2005-08-12 10:39:13 +08:00
|
|
|
|
2022-10-27 03:16:35 +08:00
|
|
|
#define nextpage(x) ((char *) roundup2 ((uintptr_t) (x), \
|
|
|
|
wincap.allocation_granularity ()))
|
|
|
|
#define allocsize(x) ((SIZE_T) nextpage (x))
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
#define somekinda_printf debug_printf
|
|
|
|
#else
|
|
|
|
#define somekinda_printf malloc_printf
|
|
|
|
#endif
|
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2002-06-16 05:59:32 +08:00
|
|
|
cygheap_fixup_in_child (bool execed)
|
2001-07-17 11:41:52 +08:00
|
|
|
{
|
2022-10-27 03:16:35 +08:00
|
|
|
SIZE_T commit_size = CYGHEAP_STORAGE_INITIAL - CYGHEAP_STORAGE_LOW;
|
|
|
|
|
|
|
|
if (child_proc_info->cygheap_max > (void *) CYGHEAP_STORAGE_INITIAL)
|
|
|
|
commit_size = allocsize (child_proc_info->cygheap_max);
|
|
|
|
cygheap = (init_cygheap *) VirtualAlloc ((LPVOID) CYGHEAP_STORAGE_LOW,
|
|
|
|
CYGHEAP_STORAGE_HIGH
|
|
|
|
- CYGHEAP_STORAGE_LOW,
|
|
|
|
MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
cygheap = (init_cygheap *) VirtualAlloc ((LPVOID) CYGHEAP_STORAGE_LOW,
|
|
|
|
commit_size, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE);
|
2023-01-10 20:04:29 +08:00
|
|
|
if (dynamically_loaded && execed)
|
|
|
|
spawn_info->moreinfo->myself_pinfo = NULL;
|
2022-10-27 03:16:35 +08:00
|
|
|
cygheap_max = child_proc_info->cygheap_max;
|
forkables: On fork failure, retry with hardlinks.
To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files. Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.
The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork
* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
(enum child_status): Add _CI_SILENTFAIL flag.
(struct child_info): Add silentfail setter and getter.
* winsup.h (child_copy): Add bool silentfail parameter.
* cygheap.cc: Pass silentfail parameter to child_copy.
* dcrt0.cc: Ditto.
* dll_init.h (struct dll): Define public inline method forkedntname.
(struct dll_list): Declare private method find_by_forkedntname.
* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
(dll_list::alloc): Use find_by_forkedntname when in load after fork.
(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
* fork.cc (frok::parent): Set silentfail child info flag. Pass
silentfail parameter to child_copy. Use forkedntname of
dlls.main_executable.
(fork): When first dofork run failed and did not use forkables,
run dofork again with_forkables set to true.
(child_copy): Use debug_printf if silentfail is true,
system_printf otherwise.
2016-12-07 18:58:28 +08:00
|
|
|
child_copy (child_proc_info->parent, false, child_proc_info->silentfail (),
|
|
|
|
"cygheap", cygheap, cygheap_max, NULL);
|
2005-07-17 08:51:03 +08:00
|
|
|
cygheap_init ();
|
|
|
|
debug_fixup_after_fork_exec ();
|
2001-07-17 11:41:52 +08:00
|
|
|
if (execed)
|
|
|
|
{
|
2005-03-23 03:00:31 +08:00
|
|
|
cygheap->hooks.next = NULL;
|
2002-10-23 00:18:55 +08:00
|
|
|
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
|
2011-11-29 23:34:49 +08:00
|
|
|
}
|
|
|
|
/* Walk the allocated memory chain looking for orphaned memory from
|
|
|
|
previous execs or forks */
|
|
|
|
for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
|
|
|
|
{
|
|
|
|
cygheap_entry *ce = (cygheap_entry *) rvc->data;
|
|
|
|
if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
|
|
|
|
continue;
|
|
|
|
else if (ce->type > HEAP_2_MAX)
|
|
|
|
_cfree (ce); /* Marked for freeing in any child */
|
|
|
|
else if (!execed)
|
|
|
|
continue;
|
|
|
|
else if (ce->type > HEAP_1_MAX)
|
|
|
|
_cfree (ce); /* Marked for freeing in execed child */
|
|
|
|
else
|
|
|
|
ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
|
2001-07-17 11:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
void
|
|
|
|
init_cygheap::close_ctty ()
|
|
|
|
{
|
|
|
|
debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
|
2022-12-20 19:26:36 +08:00
|
|
|
if (cygheap->ctty->tc ()->getsid () == pid)
|
|
|
|
cygheap->ctty->tc ()->setsid (0); /* Release CTTY ownership */
|
2011-05-06 06:30:53 +08:00
|
|
|
cygheap->ctty->close_with_arch ();
|
2004-01-24 07:05:33 +08:00
|
|
|
cygheap->ctty = NULL;
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 19:27:43 +08:00
|
|
|
/* Use absolute path of cygwin1.dll to derive the Win32 dir which
|
|
|
|
is our installation_root. Note that we can't handle Cygwin installation
|
|
|
|
root dirs of more than 4K path length. I assume that's ok...
|
|
|
|
|
|
|
|
This function also generates the installation_key value. It's a 64 bit
|
|
|
|
hash value based on the path of the Cygwin DLL itself. It's subsequently
|
|
|
|
used when generating shared object names. Thus, different Cygwin
|
|
|
|
installations generate different object names and so are isolated from
|
|
|
|
each other.
|
|
|
|
|
|
|
|
Having this information, the installation key together with the
|
|
|
|
installation root path is written to the registry. The idea is that
|
|
|
|
cygcheck can print the paths into which the Cygwin DLL has been
|
|
|
|
installed for debugging purposes.
|
|
|
|
|
2014-10-29 18:40:47 +08:00
|
|
|
Last but not least, the new cygwin properties datastructure is checked
|
2012-02-14 19:27:43 +08:00
|
|
|
for the "disabled_key" value, which is used to determine whether the
|
|
|
|
installation key is actually added to all object names or not. This is
|
|
|
|
used as a last resort for debugging purposes, usually. However, there
|
|
|
|
could be another good reason to re-enable object name collisions between
|
|
|
|
multiple Cygwin DLLs, which we're just not aware of right now. Cygcheck
|
|
|
|
can be used to change the value in an existing Cygwin DLL binary. */
|
|
|
|
void
|
|
|
|
init_cygheap::init_installation_root ()
|
|
|
|
{
|
2014-10-29 18:40:47 +08:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
2018-12-25 08:06:34 +08:00
|
|
|
if (!GetModuleFileNameW (cygwin_hmodule, installation_root_buf, PATH_MAX))
|
2012-02-14 19:27:43 +08:00
|
|
|
api_fatal ("Can't initialize Cygwin installation root dir.\n"
|
|
|
|
"GetModuleFileNameW(%p, %p, %u), %E",
|
2018-12-25 08:06:34 +08:00
|
|
|
cygwin_hmodule, installation_root_buf, PATH_MAX);
|
2021-02-20 01:15:58 +08:00
|
|
|
|
|
|
|
/* We don't care if fetching the final pathname fails, it's non-fatal and
|
|
|
|
the path returned by GetModuleFileNameW is still valid. */
|
|
|
|
HANDLE h;
|
|
|
|
h = CreateFileW (installation_root_buf, GENERIC_READ, FILE_SHARE_VALID_FLAGS,
|
|
|
|
&sec_none, OPEN_EXISTING, 0, 0);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
GetFinalPathNameByHandleW (h, installation_root_buf, PATH_MAX,
|
|
|
|
FILE_NAME_NORMALIZED);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
|
2018-12-25 08:06:34 +08:00
|
|
|
PWCHAR p = installation_root_buf;
|
2012-06-19 04:17:15 +08:00
|
|
|
if (wcsncasecmp (p, L"\\\\", 2)) /* Normal drive letter path */
|
2012-02-14 19:27:43 +08:00
|
|
|
{
|
2014-10-29 18:40:47 +08:00
|
|
|
len = 4;
|
2014-10-17 22:36:59 +08:00
|
|
|
memmove (p + 4, p, PATH_MAX - 4);
|
|
|
|
p = wcpncpy (p, L"\\\\?\\", 4);
|
2012-06-19 04:17:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool unc = false;
|
|
|
|
if (wcsncmp (p + 2, L"?\\", 2)) /* No long path prefix, so UNC path. */
|
2012-02-14 19:27:43 +08:00
|
|
|
{
|
2014-10-29 18:40:47 +08:00
|
|
|
len = 6;
|
2014-10-17 22:36:59 +08:00
|
|
|
memmove (p + 6, p, PATH_MAX - 6);
|
|
|
|
p = wcpncpy (p, L"\\??\\UN", 6);
|
2012-02-14 19:27:43 +08:00
|
|
|
*p = L'C';
|
2012-06-19 04:17:15 +08:00
|
|
|
unc = true;
|
2012-02-14 19:27:43 +08:00
|
|
|
}
|
2012-06-19 04:17:15 +08:00
|
|
|
else if (!wcsncmp (p + 4, L"UNC\\", 4)) /* Native NT UNC path. */
|
|
|
|
unc = true;
|
|
|
|
if (unc)
|
2012-02-14 19:27:43 +08:00
|
|
|
{
|
2012-06-19 04:17:15 +08:00
|
|
|
p = wcschr (p + 2, L'\\'); /* Skip server name */
|
|
|
|
if (p)
|
|
|
|
p = wcschr (p + 1, L'\\'); /* Skip share name */
|
2012-02-14 19:27:43 +08:00
|
|
|
}
|
2022-05-27 20:54:36 +08:00
|
|
|
else /* Long path prefix followed by drive letter path */
|
|
|
|
{
|
|
|
|
len = 4;
|
|
|
|
p += 4;
|
|
|
|
}
|
2012-02-14 19:27:43 +08:00
|
|
|
}
|
2018-12-25 08:06:34 +08:00
|
|
|
installation_root_buf[1] = L'?';
|
2012-02-14 19:27:43 +08:00
|
|
|
RtlInitEmptyUnicodeString (&installation_key, installation_key_buf,
|
|
|
|
sizeof installation_key_buf);
|
2018-12-25 08:06:34 +08:00
|
|
|
RtlInt64ToHexUnicodeString (hash_path_name (0, installation_root_buf),
|
2012-02-14 19:27:43 +08:00
|
|
|
&installation_key, FALSE);
|
|
|
|
|
2014-10-17 22:36:59 +08:00
|
|
|
/* Strip off last path component ("\\cygwin1.dll") */
|
2018-12-25 08:06:34 +08:00
|
|
|
PWCHAR w = wcsrchr (installation_root_buf, L'\\');
|
2012-02-14 19:27:43 +08:00
|
|
|
if (w)
|
|
|
|
{
|
|
|
|
*w = L'\0';
|
2018-12-25 08:06:34 +08:00
|
|
|
w = wcsrchr (installation_root_buf, L'\\');
|
2012-02-14 19:27:43 +08:00
|
|
|
}
|
|
|
|
if (!w)
|
|
|
|
api_fatal ("Can't initialize Cygwin installation root dir.\n"
|
|
|
|
"Invalid DLL path");
|
2014-10-15 03:14:33 +08:00
|
|
|
|
2014-10-17 22:36:59 +08:00
|
|
|
/* Copy result into installation_dir before stripping off "bin" dir and
|
2014-10-27 19:33:53 +08:00
|
|
|
revert to Win32 path. This path is added to the Windows environment
|
2014-12-02 17:59:53 +08:00
|
|
|
in build_env. See there for a description. */
|
2018-12-25 08:06:34 +08:00
|
|
|
wcpncpy (installation_dir_buf, installation_root_buf + len, PATH_MAX);
|
|
|
|
|
2014-10-29 18:40:47 +08:00
|
|
|
if (len == 4) /* Local path */
|
|
|
|
;
|
|
|
|
else if (len == 6) /* UNC path */
|
2018-12-25 08:06:34 +08:00
|
|
|
installation_dir_buf[0] = L'\\';
|
2014-10-29 18:40:47 +08:00
|
|
|
else /* Long, prefixed path */
|
2018-12-25 08:06:34 +08:00
|
|
|
installation_dir_buf[1] = L'\\';
|
2014-10-15 03:14:33 +08:00
|
|
|
|
2012-06-19 04:17:15 +08:00
|
|
|
/* If w < p, the Cygwin DLL resides in the root dir of a drive or network
|
|
|
|
path. In that case, if we strip off yet another backslash, the path
|
|
|
|
becomes invalid. We avoid that here so that the DLL also works in this
|
|
|
|
scenario. The /usr/bin and /usr/lib default mounts will probably point
|
|
|
|
to something non-existing, but that's life. */
|
|
|
|
if (w > p)
|
|
|
|
*w = L'\0';
|
2012-02-14 19:27:43 +08:00
|
|
|
|
2018-12-25 08:06:34 +08:00
|
|
|
RtlInitUnicodeString (&installation_root, installation_root_buf);
|
|
|
|
RtlInitUnicodeString (&installation_dir, installation_dir_buf);
|
|
|
|
|
2012-02-14 19:27:43 +08:00
|
|
|
for (int i = 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
reg_key r (i, KEY_WRITE, _WIDE (CYGWIN_INFO_INSTALLATIONS_NAME),
|
|
|
|
NULL);
|
|
|
|
if (NT_SUCCESS (r.set_string (installation_key_buf,
|
2018-12-25 08:06:34 +08:00
|
|
|
installation_root_buf)))
|
2012-02-14 19:27:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 23:02:05 +08:00
|
|
|
/* Initialize bucket_val. The value is the max size of a block
|
|
|
|
fitting into the bucket. The values are powers of two and their
|
|
|
|
medians: 24, 32, 48, 64, ...
|
|
|
|
The idea is to have better matching bucket sizes (not wasting
|
|
|
|
space) without trading in performance compared to the old powers
|
|
|
|
of 2 method. */
|
|
|
|
static const uint32_t bucket_val[NBUCKETS] = {
|
|
|
|
0, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048,
|
|
|
|
3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304,
|
|
|
|
131072, 196608, 262144, 393216, 524288, 786432, 1048576, 1572864, 2097152,
|
|
|
|
3145728, 4194304, 6291456, 8388608, 12582912
|
|
|
|
};
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2000-11-14 13:53:32 +08:00
|
|
|
cygheap_init ()
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.init ("cygheap_protect");
|
2011-05-30 14:24:53 +08:00
|
|
|
if (cygheap == &cygheap_dummy)
|
2002-08-07 09:20:59 +08:00
|
|
|
{
|
2022-10-27 03:16:35 +08:00
|
|
|
cygheap = (init_cygheap *) VirtualAlloc ((LPVOID) CYGHEAP_STORAGE_LOW,
|
|
|
|
CYGHEAP_STORAGE_HIGH
|
|
|
|
- CYGHEAP_STORAGE_LOW,
|
|
|
|
MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
cygheap = (init_cygheap *) VirtualAlloc ((LPVOID) CYGHEAP_STORAGE_LOW,
|
|
|
|
CYGHEAP_STORAGE_INITIAL
|
|
|
|
- CYGHEAP_STORAGE_LOW,
|
|
|
|
MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
cygheap_max = (char *) cygheap + sizeof (*cygheap);
|
2009-05-15 03:49:37 +08:00
|
|
|
/* Default locale settings. */
|
|
|
|
cygheap->locale.mbtowc = __utf8_mbtowc;
|
2009-05-06 19:52:20 +08:00
|
|
|
/* Set umask to a sane default. */
|
|
|
|
cygheap->umask = 022;
|
2010-04-20 18:44:52 +08:00
|
|
|
cygheap->rlim_core = RLIM_INFINITY;
|
2002-08-07 09:20:59 +08:00
|
|
|
}
|
2001-04-19 05:10:15 +08:00
|
|
|
if (!cygheap->fdtab)
|
|
|
|
cygheap->fdtab.init ();
|
2003-09-01 10:05:32 +08:00
|
|
|
if (!cygheap->sigs)
|
|
|
|
sigalloc ();
|
2012-08-10 03:58:53 +08:00
|
|
|
cygheap->init_tls_list ();
|
2000-11-14 13:53:32 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 22:36:59 +08:00
|
|
|
/* Initial Cygwin heap setup.
|
|
|
|
Called by root process of a Cygwin process tree. */
|
|
|
|
void
|
|
|
|
setup_cygheap ()
|
|
|
|
{
|
|
|
|
cygheap_init ();
|
|
|
|
cygheap->user.init ();
|
|
|
|
cygheap->init_installation_root (); /* Requires user.init! */
|
|
|
|
cygheap->pg.init ();
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
static void *
|
2013-04-23 17:44:36 +08:00
|
|
|
_csbrk (int sbs)
|
|
|
|
{
|
|
|
|
void *prebrk = cygheap_max;
|
|
|
|
char *newbase = nextpage (prebrk);
|
|
|
|
cygheap_max = (char *) cygheap_max + sbs;
|
2022-10-27 03:16:35 +08:00
|
|
|
if (!sbs || newbase >= cygheap_max
|
|
|
|
|| cygheap_max <= (void *) CYGHEAP_STORAGE_INITIAL)
|
2013-04-23 17:44:36 +08:00
|
|
|
/* nothing to do */;
|
|
|
|
else
|
|
|
|
{
|
2022-10-27 03:16:35 +08:00
|
|
|
if (prebrk <= (void *) CYGHEAP_STORAGE_INITIAL)
|
|
|
|
newbase = (char *) CYGHEAP_STORAGE_INITIAL;
|
2013-04-23 17:44:36 +08:00
|
|
|
|
|
|
|
SIZE_T adjsbs = allocsize ((char *) cygheap_max - newbase);
|
2022-10-27 03:16:35 +08:00
|
|
|
if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT, PAGE_READWRITE))
|
2013-04-23 17:44:36 +08:00
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
if (!VirtualQuery (newbase, &m, sizeof m))
|
|
|
|
system_printf ("couldn't get memory info, %E");
|
|
|
|
somekinda_printf ("Couldn't reserve/commit %ld bytes of space for cygwin's heap, %E",
|
|
|
|
adjsbs);
|
|
|
|
somekinda_printf ("AllocationBase %p, BaseAddress %p, RegionSize %lx, State %x\n",
|
|
|
|
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
|
|
|
|
__seterrno ();
|
|
|
|
cygheap_max = (char *) cygheap_max - sbs;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return prebrk;
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
static void *_cmalloc (unsigned size);
|
|
|
|
static void *_crealloc (void *ptr, unsigned size);
|
2000-10-17 07:55:58 +08:00
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
static void *
|
2003-01-18 11:26:07 +08:00
|
|
|
_cmalloc (unsigned size)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc;
|
2013-04-23 17:44:36 +08:00
|
|
|
unsigned b;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Calculate "bit bucket". */
|
2022-10-28 23:02:05 +08:00
|
|
|
for (b = 1; b < NBUCKETS && bucket_val[b] < size; b++)
|
2000-10-17 07:55:58 +08:00
|
|
|
continue;
|
2013-04-23 17:44:36 +08:00
|
|
|
if (b >= NBUCKETS)
|
|
|
|
return NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.acquire ();
|
2001-09-06 11:39:18 +08:00
|
|
|
if (cygheap->buckets[b])
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc = (_cmalloc_entry *) cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = rvc->ptr;
|
2000-09-30 12:54:11 +08:00
|
|
|
rvc->b = b;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2000-10-02 10:26:04 +08:00
|
|
|
else
|
|
|
|
{
|
2022-10-28 23:02:05 +08:00
|
|
|
rvc = (_cmalloc_entry *) _csbrk (bucket_val[b] + sizeof (_cmalloc_entry));
|
2002-10-09 12:08:05 +08:00
|
|
|
if (!rvc)
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2002-10-09 12:08:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-02 10:26:04 +08:00
|
|
|
rvc->b = b;
|
2000-11-14 13:53:32 +08:00
|
|
|
rvc->prev = cygheap->chain;
|
|
|
|
cygheap->chain = rvc;
|
2000-10-02 10:26:04 +08:00
|
|
|
}
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2000-09-30 12:54:11 +08:00
|
|
|
return rvc->data;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
static void
|
2000-09-03 12:16:35 +08:00
|
|
|
_cfree (void *ptr)
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.acquire ();
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
2013-04-23 17:44:36 +08:00
|
|
|
unsigned b = rvc->b;
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc->ptr = cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = (char *) rvc;
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
static void *
|
2003-01-18 11:26:07 +08:00
|
|
|
_crealloc (void *ptr, unsigned size)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2000-09-05 11:16:28 +08:00
|
|
|
void *newptr;
|
|
|
|
if (ptr == NULL)
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
else
|
|
|
|
{
|
2022-10-28 23:02:05 +08:00
|
|
|
unsigned oldsize = bucket_val[to_cmalloc (ptr)->b];
|
2000-09-05 11:16:28 +08:00
|
|
|
if (size <= oldsize)
|
|
|
|
return ptr;
|
|
|
|
newptr = _cmalloc (size);
|
2007-11-24 00:37:05 +08:00
|
|
|
if (newptr)
|
|
|
|
{
|
|
|
|
memcpy (newptr, ptr, oldsize);
|
|
|
|
_cfree (ptr);
|
|
|
|
}
|
2000-09-05 11:16:28 +08:00
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
|
2002-09-23 08:31:31 +08:00
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - offsetof (cygheap_entry, data)))
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
inline static void *
|
2007-11-27 05:30:49 +08:00
|
|
|
creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2008-09-11 12:34:24 +08:00
|
|
|
if (c)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (fn)
|
|
|
|
api_fatal ("%s would have returned NULL", fn);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
c->type = x;
|
2001-02-02 12:32:02 +08:00
|
|
|
char *cend = ((char *) c + sizeof (*c) + len);
|
|
|
|
if (cygheap_max < cend)
|
|
|
|
cygheap_max = cend;
|
2000-09-03 12:16:35 +08:00
|
|
|
return (void *) c->data;
|
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
inline static void *
|
2013-07-31 22:43:05 +08:00
|
|
|
cmalloc (cygheap_types x, size_t n, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (x, c, n, fn);
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
cmalloc (cygheap_types x, size_t n)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return cmalloc (x, n, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
cmalloc_abort (cygheap_types x, size_t n)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return cmalloc (x, n, "cmalloc");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void *
|
2013-07-31 22:43:05 +08:00
|
|
|
crealloc (void *s, size_t n, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
|
|
|
|
assert (!inheap (s));
|
|
|
|
cygheap_entry *c = tocygheap (s);
|
2000-09-30 12:54:11 +08:00
|
|
|
cygheap_types t = (cygheap_types) c->type;
|
2000-09-03 12:16:35 +08:00
|
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (t, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
crealloc (void *s, size_t n)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return crealloc (s, n, NULL);
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
crealloc_abort (void *s, size_t n)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return crealloc (s, n, "crealloc");
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void
|
2000-09-03 12:16:35 +08:00
|
|
|
cfree (void *s)
|
|
|
|
{
|
|
|
|
assert (!inheap (s));
|
2005-07-07 04:05:03 +08:00
|
|
|
_cfree (tocygheap (s));
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void
|
2002-06-28 04:44:27 +08:00
|
|
|
cfree_and_set (char *&s, char *what)
|
|
|
|
{
|
|
|
|
if (s && s != almost_null)
|
|
|
|
cfree (s);
|
|
|
|
s = what;
|
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
inline static void *
|
2013-07-31 22:43:05 +08:00
|
|
|
ccalloc (cygheap_types x, size_t n, size_t size, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2001-09-14 05:15:34 +08:00
|
|
|
n *= size;
|
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2000-09-03 12:16:35 +08:00
|
|
|
if (c)
|
2001-09-14 05:15:34 +08:00
|
|
|
memset (c->data, 0, n);
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (x, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
ccalloc (cygheap_types x, size_t n, size_t size)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, NULL);
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" void *
|
2013-07-31 22:43:05 +08:00
|
|
|
ccalloc_abort (cygheap_types x, size_t n, size_t size)
|
2007-11-27 05:30:49 +08:00
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, "ccalloc");
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" PWCHAR
|
2014-02-11 04:15:58 +08:00
|
|
|
cwcsdup (PCWSTR s)
|
2008-02-15 00:47:11 +08:00
|
|
|
{
|
2010-04-13 18:01:34 +08:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-15 00:47:11 +08:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" PWCHAR
|
2014-02-11 04:15:58 +08:00
|
|
|
cwcsdup1 (PCWSTR s)
|
2008-02-15 00:47:11 +08:00
|
|
|
{
|
2010-04-13 18:01:34 +08:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-15 00:47:11 +08:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" char *
|
2000-09-03 12:16:35 +08:00
|
|
|
cstrdup (const char *s)
|
|
|
|
{
|
|
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
|
|
|
return p;
|
|
|
|
}
|
2000-09-27 13:12:09 +08:00
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
extern "C" char *
|
2000-09-27 13:12:09 +08:00
|
|
|
cstrdup1 (const char *s)
|
|
|
|
{
|
|
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
|
|
|
return p;
|
|
|
|
}
|
2000-11-15 08:13:09 +08:00
|
|
|
|
2001-06-03 10:31:16 +08:00
|
|
|
void
|
2008-07-17 04:20:45 +08:00
|
|
|
cygheap_root::set (const char *posix, const char *native, bool caseinsensitive)
|
2000-11-15 08:13:09 +08:00
|
|
|
{
|
2001-08-07 08:01:42 +08:00
|
|
|
if (*posix == '/' && posix[1] == '\0')
|
|
|
|
{
|
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
cfree (m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-06-03 10:31:16 +08:00
|
|
|
if (!m)
|
|
|
|
m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
|
|
|
|
strcpy (m->posix_path, posix);
|
|
|
|
m->posix_pathlen = strlen (posix);
|
|
|
|
if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
|
|
|
|
m->posix_path[--m->posix_pathlen] = '\0';
|
|
|
|
|
|
|
|
strcpy (m->native_path, native);
|
|
|
|
m->native_pathlen = strlen (native);
|
|
|
|
if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
|
|
|
|
m->native_path[--m->native_pathlen] = '\0';
|
2008-07-17 04:20:45 +08:00
|
|
|
m->caseinsensitive = caseinsensitive;
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cygheap_user::~cygheap_user ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cygheap_user::set_name (const char *new_name)
|
|
|
|
{
|
2002-06-27 03:39:05 +08:00
|
|
|
bool allocated = !!pname;
|
2002-06-27 10:29:57 +08:00
|
|
|
|
2002-06-27 03:39:05 +08:00
|
|
|
if (allocated)
|
2002-06-27 10:29:57 +08:00
|
|
|
{
|
2010-06-01 02:52:02 +08:00
|
|
|
/* Windows user names are case-insensitive. Here we want the correct
|
2011-06-06 13:02:13 +08:00
|
|
|
username, though, even if it only differs by case. */
|
2010-06-01 02:52:02 +08:00
|
|
|
if (!strcmp (new_name, pname))
|
2002-06-27 10:29:57 +08:00
|
|
|
return;
|
|
|
|
cfree (pname);
|
|
|
|
}
|
2002-06-27 03:39:05 +08:00
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
pname = cstrdup (new_name ? new_name : "");
|
2002-06-27 03:39:05 +08:00
|
|
|
if (!allocated)
|
|
|
|
return; /* Initializing. Don't bother with other stuff. */
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
cfree_and_set (homedrive);
|
|
|
|
cfree_and_set (homepath);
|
|
|
|
cfree_and_set (plogsrv);
|
|
|
|
cfree_and_set (pdomain);
|
2002-06-29 10:36:08 +08:00
|
|
|
cfree_and_set (pwinname);
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
2012-08-10 03:58:53 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
init_cygheap::init_tls_list ()
|
|
|
|
{
|
|
|
|
if (threadlist)
|
|
|
|
memset (cygheap->threadlist, 0, cygheap->sthreads * sizeof (cygheap->threadlist[0]));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sthreads = THREADLIST_CHUNK;
|
2014-11-29 04:46:13 +08:00
|
|
|
threadlist = (threadlist_t *)
|
|
|
|
ccalloc_abort (HEAP_TLS, cygheap->sthreads,
|
|
|
|
sizeof (cygheap->threadlist[0]));
|
2012-08-10 03:58:53 +08:00
|
|
|
}
|
|
|
|
tls_sentry::lock.init ("thread_tls_sentry");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_cygheap::add_tls (_cygtls *t)
|
|
|
|
{
|
|
|
|
cygheap->user.reimpersonate ();
|
|
|
|
tls_sentry here (INFINITE);
|
|
|
|
if (nthreads >= cygheap->sthreads)
|
|
|
|
{
|
2014-11-29 04:46:13 +08:00
|
|
|
threadlist = (threadlist_t *)
|
2012-08-10 03:58:53 +08:00
|
|
|
crealloc_abort (threadlist, (sthreads += THREADLIST_CHUNK)
|
|
|
|
* sizeof (threadlist[0]));
|
2014-11-29 04:46:13 +08:00
|
|
|
#if 0
|
|
|
|
memset (threadlist + nthreads, 0,
|
|
|
|
THREADLIST_CHUNK * sizeof (threadlist[0]));
|
|
|
|
#endif
|
2012-08-10 03:58:53 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
/* Create a mutex to lock the thread's _cygtls area. This is required for
|
|
|
|
the following reason: The thread's _cygtls area is on the thread's
|
|
|
|
own stack. Thus, when the thread exits, its _cygtls area is automatically
|
|
|
|
destroyed by the OS. Thus, when this happens while the signal thread
|
|
|
|
still utilizes the thread's _cygtls area, things go awry.
|
|
|
|
|
|
|
|
The following methods take this into account:
|
2020-03-11 20:28:27 +08:00
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
- The thread mutex is generally only locked under tls_sentry locking.
|
|
|
|
- remove_tls, called from _cygtls::remove, locks the mutex before
|
|
|
|
removing the threadlist entry and _cygtls::remove then unlocks and
|
|
|
|
destroyes the mutex.
|
|
|
|
- find_tls, called from several places but especially from the signal
|
|
|
|
thread, will lock the mutex on exit and the caller can access the
|
|
|
|
_cygtls area locked. Always make sure to unlock the mutex when the
|
|
|
|
_cygtls area isn't needed anymore. */
|
|
|
|
threadlist[nthreads].thread = t;
|
|
|
|
threadlist[nthreads].mutex = CreateMutexW (&sec_none_nih, FALSE, NULL);
|
|
|
|
if (!threadlist[nthreads].mutex)
|
|
|
|
api_fatal ("Can't create per-thread mutex, %E");
|
|
|
|
++nthreads;
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
HANDLE
|
2014-11-29 04:46:13 +08:00
|
|
|
init_cygheap::remove_tls (_cygtls *t)
|
|
|
|
{
|
|
|
|
HANDLE mutex = NULL;
|
2012-08-10 03:58:53 +08:00
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
tls_sentry here (INFINITE);
|
2012-08-10 03:58:53 +08:00
|
|
|
if (here.acquired ())
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
for (uint32_t i = 0; i < nthreads; i++)
|
2014-11-29 04:46:13 +08:00
|
|
|
if (t == threadlist[i].thread)
|
2012-08-10 03:58:53 +08:00
|
|
|
{
|
2014-11-29 04:46:13 +08:00
|
|
|
mutex = threadlist[i].mutex;
|
|
|
|
WaitForSingleObject (mutex, INFINITE);
|
2012-08-10 03:58:53 +08:00
|
|
|
if (i < --nthreads)
|
|
|
|
threadlist[i] = threadlist[nthreads];
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_only_printf ("removed %p element %u", this, i);
|
2012-08-10 03:58:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 04:46:13 +08:00
|
|
|
/* Leave with locked mutex. The calling function is responsible for
|
|
|
|
unlocking the mutex. */
|
|
|
|
return mutex;
|
2012-08-10 03:58:53 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
threadlist_t *
|
2014-11-29 04:46:13 +08:00
|
|
|
init_cygheap::find_tls (_cygtls *tls)
|
|
|
|
{
|
|
|
|
tls_sentry here (INFINITE);
|
|
|
|
|
|
|
|
threadlist_t *t = NULL;
|
|
|
|
int ix = -1;
|
|
|
|
while (++ix < (int) nthreads)
|
|
|
|
{
|
|
|
|
if (!threadlist[ix].thread->tid
|
|
|
|
|| !threadlist[ix].thread->initialized)
|
|
|
|
;
|
|
|
|
if (threadlist[ix].thread == tls)
|
|
|
|
{
|
|
|
|
t = &threadlist[ix];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Leave with locked mutex. The calling function is responsible for
|
|
|
|
unlocking the mutex. */
|
|
|
|
if (t)
|
|
|
|
WaitForSingleObject (t->mutex, INFINITE);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
threadlist_t *
|
2013-03-31 20:35:44 +08:00
|
|
|
init_cygheap::find_tls (int sig, bool& issig_wait)
|
2012-08-10 03:58:53 +08:00
|
|
|
{
|
|
|
|
debug_printf ("sig %d\n", sig);
|
|
|
|
tls_sentry here (INFINITE);
|
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
threadlist_t *t = NULL;
|
2013-03-31 20:35:44 +08:00
|
|
|
issig_wait = false;
|
2012-08-10 03:58:53 +08:00
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
int ix = -1;
|
2014-11-28 00:20:14 +08:00
|
|
|
/* Scan thread list looking for valid signal-delivery candidates */
|
|
|
|
while (++ix < (int) nthreads)
|
2012-08-10 03:58:53 +08:00
|
|
|
{
|
2014-11-29 04:46:13 +08:00
|
|
|
/* Only pthreads have tid set to non-0. */
|
|
|
|
if (!threadlist[ix].thread->tid
|
|
|
|
|| !threadlist[ix].thread->initialized)
|
|
|
|
;
|
|
|
|
else if (sigismember (&(threadlist[ix].thread->sigwait_mask), sig))
|
2014-11-28 00:20:14 +08:00
|
|
|
{
|
2014-11-29 04:46:13 +08:00
|
|
|
t = &cygheap->threadlist[ix];
|
|
|
|
issig_wait = true;
|
|
|
|
break;
|
2014-11-28 00:20:14 +08:00
|
|
|
}
|
2014-11-29 04:46:13 +08:00
|
|
|
else if (!t && !sigismember (&(threadlist[ix].thread->sigmask), sig))
|
|
|
|
t = &cygheap->threadlist[ix];
|
2014-08-22 17:21:33 +08:00
|
|
|
}
|
2014-11-29 04:46:13 +08:00
|
|
|
/* Leave with locked mutex. The calling function is responsible for
|
|
|
|
unlocking the mutex. */
|
|
|
|
if (t)
|
|
|
|
WaitForSingleObject (t->mutex, INFINITE);
|
2012-08-10 03:58:53 +08:00
|
|
|
return t;
|
|
|
|
}
|
2016-02-20 14:58:31 +08:00
|
|
|
|
2022-03-01 23:13:20 +08:00
|
|
|
sigset_t
|
|
|
|
init_cygheap::compute_sigblkmask ()
|
|
|
|
{
|
|
|
|
sigset_t ret_mask = -1;
|
|
|
|
|
|
|
|
tls_sentry here (INFINITE);
|
|
|
|
|
|
|
|
int ix = -1;
|
|
|
|
/* Scan thread list looking for valid signal-receiving threads */
|
|
|
|
while (++ix < (int) nthreads)
|
|
|
|
{
|
|
|
|
/* Only pthreads have tid set to non-0. */
|
|
|
|
if (threadlist[ix].thread->tid && threadlist[ix].thread->initialized)
|
|
|
|
ret_mask &= threadlist[ix].thread->sigmask;
|
|
|
|
}
|
|
|
|
return ret_mask;
|
|
|
|
}
|
|
|
|
|
2016-02-20 14:58:31 +08:00
|
|
|
/* Called from profil.c to sample all non-main thread PC values for profiling */
|
|
|
|
extern "C" void
|
|
|
|
cygheap_profthr_all (void (*profthr_byhandle) (HANDLE))
|
|
|
|
{
|
|
|
|
for (uint32_t ix = 0; ix < nthreads; ix++)
|
|
|
|
{
|
|
|
|
_cygtls *tls = cygheap->threadlist[ix].thread;
|
|
|
|
if (tls->tid)
|
|
|
|
profthr_byhandle (tls->tid->win32_obj_id);
|
|
|
|
}
|
|
|
|
}
|