2000-09-03 06:16:35 +02:00
|
|
|
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2014-02-10 21:15:58 +01:00
|
|
|
2011, 2012, 2013, 2014 Red Hat, Inc.
|
2000-09-03 06:16:35 +02: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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <assert.h>
|
2000-10-02 04:26:04 +02:00
|
|
|
#include <stdlib.h>
|
2005-01-12 23:40:46 +01:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-09-09 18:52:37 +02:00
|
|
|
#include "path.h"
|
2011-04-21 02:53:55 +02: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 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "dtable.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
#include "cygheap.h"
|
2001-07-17 05:41:52 +02:00
|
|
|
#include "child_info.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
#include "heap.h"
|
2003-09-01 04:05:32 +02:00
|
|
|
#include "sigproc.h"
|
2005-06-01 05:46:56 +02:00
|
|
|
#include "pinfo.h"
|
2012-02-14 12:27:43 +01:00
|
|
|
#include "registry.h"
|
|
|
|
#include "ntdll.h"
|
2005-06-01 05:46:56 +02:00
|
|
|
#include <unistd.h>
|
2008-02-14 17:47:11 +01:00
|
|
|
#include <wchar.h>
|
2013-04-23 11:44:36 +02:00
|
|
|
#include <sys/param.h>
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2011-05-30 08:24:53 +02:00
|
|
|
static mini_cygheap NO_COPY cygheap_dummy =
|
2009-06-08 05:53:40 +02:00
|
|
|
{
|
|
|
|
{__utf8_mbtowc, __utf8_wctomb}
|
|
|
|
};
|
|
|
|
|
2011-05-30 08:24:53 +02:00
|
|
|
init_cygheap NO_COPY *cygheap = (init_cygheap *) &cygheap_dummy;
|
2002-10-14 22:25:52 +02:00
|
|
|
void NO_COPY *cygheap_max;
|
2000-09-30 06:54:11 +02:00
|
|
|
|
2005-06-01 06:00:40 +02:00
|
|
|
extern "C" char _cygheap_end[];
|
2005-06-01 05:46:56 +02:00
|
|
|
|
2005-04-05 06:31:00 +02:00
|
|
|
static NO_COPY muto cygheap_protect;
|
2000-10-02 04:26:04 +02:00
|
|
|
|
2001-07-17 05:41:52 +02:00
|
|
|
struct cygheap_entry
|
2009-06-08 05:53:40 +02:00
|
|
|
{
|
|
|
|
int type;
|
|
|
|
struct cygheap_entry *next;
|
|
|
|
char data[0];
|
|
|
|
};
|
2001-07-17 05:41:52 +02:00
|
|
|
|
2012-08-09 21:58:53 +02: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 11:44:36 +02:00
|
|
|
static NO_COPY uint32_t nthreads;
|
2012-08-09 21:58:53 +02:00
|
|
|
|
|
|
|
#define THREADLIST_CHUNK 256
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - offsetof (_cmalloc_entry, data)))
|
2001-07-17 05:41:52 +02:00
|
|
|
|
2001-08-04 23:10:52 +02:00
|
|
|
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
|
|
|
|
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
|
|
|
|
|
2001-07-17 05:41:52 +02:00
|
|
|
extern "C" {
|
2013-01-21 05:34:52 +01:00
|
|
|
static void __reg1 _cfree (void *);
|
2005-06-01 05:46:56 +02:00
|
|
|
static void *__stdcall _csbrk (int);
|
2001-08-04 23:10:52 +02:00
|
|
|
}
|
2005-08-12 04:39:13 +02:00
|
|
|
|
2001-07-17 05:41:52 +02:00
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
|
|
|
void __stdcall
|
2002-06-15 23:59:32 +02:00
|
|
|
cygheap_fixup_in_child (bool execed)
|
2001-07-17 05:41:52 +02:00
|
|
|
{
|
2011-05-30 08:24:53 +02:00
|
|
|
cygheap_max = cygheap = (init_cygheap *) _cygheap_start;
|
2005-06-01 05:46:56 +02:00
|
|
|
_csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
|
2005-12-17 05:36:39 +01:00
|
|
|
child_copy (child_proc_info->parent, false, "cygheap", cygheap, cygheap_max, NULL);
|
2005-07-17 02:51:03 +02:00
|
|
|
cygheap_init ();
|
|
|
|
debug_fixup_after_fork_exec ();
|
2001-07-17 05:41:52 +02:00
|
|
|
if (execed)
|
|
|
|
{
|
2005-03-22 20:00:31 +01:00
|
|
|
cygheap->hooks.next = NULL;
|
2002-10-22 18:18:55 +02:00
|
|
|
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
|
2011-11-29 16:34:49 +01: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 05:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
void
|
|
|
|
init_cygheap::close_ctty ()
|
|
|
|
{
|
|
|
|
debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
|
2011-05-06 00:30:53 +02:00
|
|
|
cygheap->ctty->close_with_arch ();
|
2004-01-24 00:05:33 +01:00
|
|
|
cygheap->ctty = NULL;
|
2004-01-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
|
2012-02-14 12:27:43 +01: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 11:40:47 +01:00
|
|
|
Last but not least, the new cygwin properties datastructure is checked
|
2012-02-14 12:27:43 +01: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 11:40:47 +01:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
2012-02-14 12:27:43 +01:00
|
|
|
if (!GetModuleFileNameW (cygwin_hmodule, installation_root, PATH_MAX))
|
|
|
|
api_fatal ("Can't initialize Cygwin installation root dir.\n"
|
|
|
|
"GetModuleFileNameW(%p, %p, %u), %E",
|
|
|
|
cygwin_hmodule, installation_root, PATH_MAX);
|
|
|
|
PWCHAR p = installation_root;
|
2012-06-18 22:17:15 +02:00
|
|
|
if (wcsncasecmp (p, L"\\\\", 2)) /* Normal drive letter path */
|
2012-02-14 12:27:43 +01:00
|
|
|
{
|
2014-10-29 11:40:47 +01:00
|
|
|
len = 4;
|
2014-10-17 16:36:59 +02:00
|
|
|
memmove (p + 4, p, PATH_MAX - 4);
|
|
|
|
p = wcpncpy (p, L"\\\\?\\", 4);
|
2012-06-18 22:17:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool unc = false;
|
|
|
|
if (wcsncmp (p + 2, L"?\\", 2)) /* No long path prefix, so UNC path. */
|
2012-02-14 12:27:43 +01:00
|
|
|
{
|
2014-10-29 11:40:47 +01:00
|
|
|
len = 6;
|
2014-10-17 16:36:59 +02:00
|
|
|
memmove (p + 6, p, PATH_MAX - 6);
|
|
|
|
p = wcpncpy (p, L"\\??\\UN", 6);
|
2012-02-14 12:27:43 +01:00
|
|
|
*p = L'C';
|
2012-06-18 22:17:15 +02:00
|
|
|
unc = true;
|
2012-02-14 12:27:43 +01:00
|
|
|
}
|
2012-06-18 22:17:15 +02:00
|
|
|
else if (!wcsncmp (p + 4, L"UNC\\", 4)) /* Native NT UNC path. */
|
|
|
|
unc = true;
|
|
|
|
if (unc)
|
2012-02-14 12:27:43 +01:00
|
|
|
{
|
2012-06-18 22:17:15 +02:00
|
|
|
p = wcschr (p + 2, L'\\'); /* Skip server name */
|
|
|
|
if (p)
|
|
|
|
p = wcschr (p + 1, L'\\'); /* Skip share name */
|
2012-02-14 12:27:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
installation_root[1] = L'?';
|
|
|
|
RtlInitEmptyUnicodeString (&installation_key, installation_key_buf,
|
|
|
|
sizeof installation_key_buf);
|
|
|
|
RtlInt64ToHexUnicodeString (hash_path_name (0, installation_root),
|
|
|
|
&installation_key, FALSE);
|
|
|
|
|
2014-10-17 16:36:59 +02:00
|
|
|
/* Strip off last path component ("\\cygwin1.dll") */
|
2012-02-14 12:27:43 +01:00
|
|
|
PWCHAR w = wcsrchr (installation_root, L'\\');
|
|
|
|
if (w)
|
|
|
|
{
|
|
|
|
*w = L'\0';
|
|
|
|
w = wcsrchr (installation_root, L'\\');
|
|
|
|
}
|
|
|
|
if (!w)
|
|
|
|
api_fatal ("Can't initialize Cygwin installation root dir.\n"
|
|
|
|
"Invalid DLL path");
|
2014-10-14 21:14:33 +02:00
|
|
|
|
2014-10-17 16:36:59 +02:00
|
|
|
/* Copy result into installation_dir before stripping off "bin" dir and
|
2014-10-27 12:33:53 +01:00
|
|
|
revert to Win32 path. This path is added to the Windows environment
|
2014-12-02 10:59:53 +01:00
|
|
|
in build_env. See there for a description. */
|
2014-10-29 11:40:47 +01:00
|
|
|
installation_dir_len = wcpncpy (installation_dir, installation_root + len,
|
2014-11-03 16:30:59 +01:00
|
|
|
PATH_MAX)
|
2014-10-27 12:33:53 +01:00
|
|
|
- installation_dir;
|
2014-10-29 11:40:47 +01:00
|
|
|
if (len == 4) /* Local path */
|
|
|
|
;
|
|
|
|
else if (len == 6) /* UNC path */
|
|
|
|
installation_dir[0] = L'\\';
|
|
|
|
else /* Long, prefixed path */
|
|
|
|
installation_dir[1] = L'\\';
|
2014-10-14 21:14:33 +02:00
|
|
|
|
2012-06-18 22:17:15 +02: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 12:27:43 +01: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,
|
|
|
|
installation_root)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cygwin_props.disable_key)
|
|
|
|
{
|
|
|
|
installation_key.Length = 0;
|
2014-11-03 16:30:59 +01:00
|
|
|
installation_key.Buffer[0] = L'\0';
|
|
|
|
}
|
2012-02-14 12:27:43 +01:00
|
|
|
}
|
|
|
|
|
2009-08-01 21:52:46 +02:00
|
|
|
void __stdcall
|
2000-11-14 06:53:32 +01:00
|
|
|
cygheap_init ()
|
|
|
|
{
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.init ("cygheap_protect");
|
2011-05-30 08:24:53 +02:00
|
|
|
if (cygheap == &cygheap_dummy)
|
2002-08-07 03:20:59 +02:00
|
|
|
{
|
2007-03-29 18:37:36 +02:00
|
|
|
cygheap = (init_cygheap *) memset (_cygheap_start, 0,
|
2011-02-11 17:05:26 +01:00
|
|
|
sizeof (*cygheap));
|
2005-06-01 05:46:56 +02:00
|
|
|
cygheap_max = cygheap;
|
2005-07-06 22:05:03 +02:00
|
|
|
_csbrk (sizeof (*cygheap));
|
2013-04-23 11:44:36 +02: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: 12, 16, 24, 32, 48, 64, ... On 64 bit, start with 24 to
|
|
|
|
accommodate bigger size of struct cygheap_entry.
|
|
|
|
With NBUCKETS == 40, the maximum block size is 6291456/12582912.
|
|
|
|
The idea is to have better matching bucket sizes (not wasting
|
|
|
|
space) without trading in performance compared to the old powers
|
|
|
|
of 2 method. */
|
|
|
|
#ifdef __x86_64__
|
|
|
|
unsigned sz[2] = { 16, 24 }; /* sizeof cygheap_entry == 16 */
|
|
|
|
#else
|
|
|
|
unsigned sz[2] = { 8, 12 }; /* sizeof cygheap_entry == 8 */
|
|
|
|
#endif
|
|
|
|
for (unsigned b = 1; b < NBUCKETS; b++, sz[b & 1] <<= 1)
|
|
|
|
cygheap->bucket_val[b] = sz[b & 1];
|
2009-05-14 21:49:37 +02:00
|
|
|
/* Default locale settings. */
|
|
|
|
cygheap->locale.mbtowc = __utf8_mbtowc;
|
|
|
|
cygheap->locale.wctomb = __utf8_wctomb;
|
2009-09-28 12:43:49 +02:00
|
|
|
strcpy (cygheap->locale.charset, "UTF-8");
|
2009-05-06 13:52:20 +02:00
|
|
|
/* Set umask to a sane default. */
|
|
|
|
cygheap->umask = 022;
|
2010-04-20 12:44:52 +02:00
|
|
|
cygheap->rlim_core = RLIM_INFINITY;
|
2002-08-07 03:20:59 +02:00
|
|
|
}
|
2001-04-18 23:10:15 +02:00
|
|
|
if (!cygheap->fdtab)
|
|
|
|
cygheap->fdtab.init ();
|
2003-09-01 04:05:32 +02:00
|
|
|
if (!cygheap->sigs)
|
|
|
|
sigalloc ();
|
2012-08-09 21:58:53 +02:00
|
|
|
cygheap->init_tls_list ();
|
2000-11-14 06:53:32 +01:00
|
|
|
}
|
|
|
|
|
2014-10-17 16:36:59 +02: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 ();
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02: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
|
|
|
|
|
|
|
|
static void *__stdcall
|
|
|
|
_csbrk (int sbs)
|
|
|
|
{
|
|
|
|
void *prebrk = cygheap_max;
|
|
|
|
char *newbase = nextpage (prebrk);
|
|
|
|
cygheap_max = (char *) cygheap_max + sbs;
|
|
|
|
if (!sbs || (newbase >= cygheap_max) || (cygheap_max <= _cygheap_end))
|
|
|
|
/* nothing to do */;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (prebrk <= _cygheap_end)
|
|
|
|
newbase = _cygheap_end;
|
|
|
|
|
|
|
|
SIZE_T adjsbs = allocsize ((char *) cygheap_max - newbase);
|
|
|
|
if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
|
|
|
|
{
|
|
|
|
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 06:16:35 +02:00
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
static void *__reg1 _cmalloc (unsigned size);
|
|
|
|
static void *__reg2 _crealloc (void *ptr, unsigned size);
|
2000-10-17 01:55:58 +02:00
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
static void *__reg1
|
2003-01-18 04:26:07 +01:00
|
|
|
_cmalloc (unsigned size)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2000-09-30 06:54:11 +02:00
|
|
|
_cmalloc_entry *rvc;
|
2013-04-23 11:44:36 +02:00
|
|
|
unsigned b;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
/* Calculate "bit bucket". */
|
|
|
|
for (b = 1; b < NBUCKETS && cygheap->bucket_val[b] < size; b++)
|
2000-10-17 01:55:58 +02:00
|
|
|
continue;
|
2013-04-23 11:44:36 +02:00
|
|
|
if (b >= NBUCKETS)
|
|
|
|
return NULL;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.acquire ();
|
2001-09-06 05:39:18 +02:00
|
|
|
if (cygheap->buckets[b])
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2001-09-06 05:39:18 +02:00
|
|
|
rvc = (_cmalloc_entry *) cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = rvc->ptr;
|
2000-09-30 06:54:11 +02:00
|
|
|
rvc->b = b;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2000-10-02 04:26:04 +02:00
|
|
|
else
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
rvc = (_cmalloc_entry *) _csbrk (cygheap->bucket_val[b]
|
|
|
|
+ sizeof (_cmalloc_entry));
|
2002-10-09 06:08:05 +02:00
|
|
|
if (!rvc)
|
|
|
|
{
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.release ();
|
2002-10-09 06:08:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-02 04:26:04 +02:00
|
|
|
rvc->b = b;
|
2000-11-14 06:53:32 +01:00
|
|
|
rvc->prev = cygheap->chain;
|
|
|
|
cygheap->chain = rvc;
|
2000-10-02 04:26:04 +02:00
|
|
|
}
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.release ();
|
2000-09-30 06:54:11 +02:00
|
|
|
return rvc->data;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
static void __reg1
|
2000-09-03 06:16:35 +02:00
|
|
|
_cfree (void *ptr)
|
|
|
|
{
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.acquire ();
|
2000-09-30 06:54:11 +02:00
|
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
2013-04-23 11:44:36 +02:00
|
|
|
unsigned b = rvc->b;
|
2001-09-06 05:39:18 +02:00
|
|
|
rvc->ptr = cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = (char *) rvc;
|
2005-04-05 06:31:00 +02:00
|
|
|
cygheap_protect.release ();
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
static void *__reg2
|
2003-01-18 04:26:07 +01:00
|
|
|
_crealloc (void *ptr, unsigned size)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2000-09-05 05:16:28 +02:00
|
|
|
void *newptr;
|
|
|
|
if (ptr == NULL)
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
else
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
unsigned oldsize = cygheap->bucket_val[to_cmalloc (ptr)->b];
|
2000-09-05 05:16:28 +02:00
|
|
|
if (size <= oldsize)
|
|
|
|
return ptr;
|
|
|
|
newptr = _cmalloc (size);
|
2007-11-23 17:37:05 +01:00
|
|
|
if (newptr)
|
|
|
|
{
|
|
|
|
memcpy (newptr, ptr, oldsize);
|
|
|
|
_cfree (ptr);
|
|
|
|
}
|
2000-09-05 05:16:28 +02:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
|
2002-09-23 02:31:31 +02:00
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - offsetof (cygheap_entry, data)))
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
inline static void *
|
2007-11-26 22:30:49 +01:00
|
|
|
creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2008-09-11 06:34:24 +02: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 06:16:35 +02:00
|
|
|
c->type = x;
|
2001-02-02 05:32:02 +01:00
|
|
|
char *cend = ((char *) c + sizeof (*c) + len);
|
|
|
|
if (cygheap_max < cend)
|
|
|
|
cygheap_max = cend;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
return (void *) c->data;
|
|
|
|
}
|
|
|
|
|
2007-11-26 22:30:49 +01:00
|
|
|
inline static void *
|
2013-07-31 16:43:05 +02:00
|
|
|
cmalloc (cygheap_types x, size_t n, const char *fn)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2007-11-26 22:30:49 +01:00
|
|
|
return creturn (x, c, n, fn);
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2007-11-26 22:30:49 +01:00
|
|
|
extern "C" void *
|
2013-07-31 16:43:05 +02:00
|
|
|
cmalloc (cygheap_types x, size_t n)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return cmalloc (x, n, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *
|
2013-07-31 16:43:05 +02:00
|
|
|
cmalloc_abort (cygheap_types x, size_t n)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return cmalloc (x, n, "cmalloc");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void *
|
2013-07-31 16:43:05 +02:00
|
|
|
crealloc (void *s, size_t n, const char *fn)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (s == NULL)
|
|
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
|
|
|
|
assert (!inheap (s));
|
|
|
|
cygheap_entry *c = tocygheap (s);
|
2000-09-30 06:54:11 +02:00
|
|
|
cygheap_types t = (cygheap_types) c->type;
|
2000-09-03 06:16:35 +02:00
|
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
2007-11-26 22:30:49 +01:00
|
|
|
return creturn (t, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void *__reg2
|
2013-07-31 16:43:05 +02:00
|
|
|
crealloc (void *s, size_t n)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return crealloc (s, n, NULL);
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void *__reg2
|
2013-07-31 16:43:05 +02:00
|
|
|
crealloc_abort (void *s, size_t n)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return crealloc (s, n, "crealloc");
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void __reg1
|
2000-09-03 06:16:35 +02:00
|
|
|
cfree (void *s)
|
|
|
|
{
|
|
|
|
assert (!inheap (s));
|
2005-07-06 22:05:03 +02:00
|
|
|
_cfree (tocygheap (s));
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void __reg2
|
2002-06-27 22:44:27 +02:00
|
|
|
cfree_and_set (char *&s, char *what)
|
|
|
|
{
|
|
|
|
if (s && s != almost_null)
|
|
|
|
cfree (s);
|
|
|
|
s = what;
|
|
|
|
}
|
|
|
|
|
2007-11-26 22:30:49 +01:00
|
|
|
inline static void *
|
2013-07-31 16:43:05 +02:00
|
|
|
ccalloc (cygheap_types x, size_t n, size_t size, const char *fn)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2001-09-13 23:15:34 +02:00
|
|
|
n *= size;
|
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2000-09-03 06:16:35 +02:00
|
|
|
if (c)
|
2001-09-13 23:15:34 +02:00
|
|
|
memset (c->data, 0, n);
|
2007-11-26 22:30:49 +01:00
|
|
|
return creturn (x, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void *__reg3
|
2013-07-31 16:43:05 +02:00
|
|
|
ccalloc (cygheap_types x, size_t n, size_t size)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, NULL);
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" void *__reg3
|
2013-07-31 16:43:05 +02:00
|
|
|
ccalloc_abort (cygheap_types x, size_t n, size_t size)
|
2007-11-26 22:30:49 +01:00
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, "ccalloc");
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" PWCHAR __reg1
|
2014-02-10 21:15:58 +01:00
|
|
|
cwcsdup (PCWSTR s)
|
2008-02-14 17:47:11 +01:00
|
|
|
{
|
|
|
|
MALLOC_CHECK;
|
2010-04-13 12:01:34 +02:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-14 17:47:11 +01:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
MALLOC_CHECK;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" PWCHAR __reg1
|
2014-02-10 21:15:58 +01:00
|
|
|
cwcsdup1 (PCWSTR s)
|
2008-02-14 17:47:11 +01:00
|
|
|
{
|
|
|
|
MALLOC_CHECK;
|
2010-04-13 12:01:34 +02:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-14 17:47:11 +01:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
MALLOC_CHECK;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" char *__reg1
|
2000-09-03 06:16:35 +02:00
|
|
|
cstrdup (const char *s)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
return p;
|
|
|
|
}
|
2000-09-27 07:12:09 +02:00
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
extern "C" char *__reg1
|
2000-09-27 07:12:09 +02:00
|
|
|
cstrdup1 (const char *s)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 07:12:09 +02:00
|
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 07:12:09 +02:00
|
|
|
return p;
|
|
|
|
}
|
2000-11-15 01:13:09 +01:00
|
|
|
|
2001-06-03 04:31:16 +02:00
|
|
|
void
|
2008-07-16 22:20:45 +02:00
|
|
|
cygheap_root::set (const char *posix, const char *native, bool caseinsensitive)
|
2000-11-15 01:13:09 +01:00
|
|
|
{
|
2001-08-07 02:01:42 +02:00
|
|
|
if (*posix == '/' && posix[1] == '\0')
|
|
|
|
{
|
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
cfree (m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-06-03 04:31:16 +02: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-16 22:20:45 +02:00
|
|
|
m->caseinsensitive = caseinsensitive;
|
2000-11-15 01:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cygheap_user::~cygheap_user ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cygheap_user::set_name (const char *new_name)
|
|
|
|
{
|
2002-06-26 21:39:05 +02:00
|
|
|
bool allocated = !!pname;
|
2002-06-27 04:29:57 +02:00
|
|
|
|
2002-06-26 21:39:05 +02:00
|
|
|
if (allocated)
|
2002-06-27 04:29:57 +02:00
|
|
|
{
|
2010-05-31 20:52:02 +02:00
|
|
|
/* Windows user names are case-insensitive. Here we want the correct
|
2011-06-06 07:02:13 +02:00
|
|
|
username, though, even if it only differs by case. */
|
2010-05-31 20:52:02 +02:00
|
|
|
if (!strcmp (new_name, pname))
|
2002-06-27 04:29:57 +02:00
|
|
|
return;
|
|
|
|
cfree (pname);
|
|
|
|
}
|
2002-06-26 21:39:05 +02:00
|
|
|
|
2000-11-15 01:13:09 +01:00
|
|
|
pname = cstrdup (new_name ? new_name : "");
|
2002-06-26 21:39:05 +02:00
|
|
|
if (!allocated)
|
|
|
|
return; /* Initializing. Don't bother with other stuff. */
|
|
|
|
|
2002-06-27 22:44:27 +02:00
|
|
|
cfree_and_set (homedrive);
|
|
|
|
cfree_and_set (homepath);
|
|
|
|
cfree_and_set (plogsrv);
|
|
|
|
cfree_and_set (pdomain);
|
2002-06-29 04:36:08 +02:00
|
|
|
cfree_and_set (pwinname);
|
2000-11-15 01:13:09 +01:00
|
|
|
}
|
2012-08-09 21:58:53 +02: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-28 21:46:13 +01:00
|
|
|
threadlist = (threadlist_t *)
|
|
|
|
ccalloc_abort (HEAP_TLS, cygheap->sthreads,
|
|
|
|
sizeof (cygheap->threadlist[0]));
|
2012-08-09 21:58:53 +02: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-28 21:46:13 +01:00
|
|
|
threadlist = (threadlist_t *)
|
2012-08-09 21:58:53 +02:00
|
|
|
crealloc_abort (threadlist, (sthreads += THREADLIST_CHUNK)
|
|
|
|
* sizeof (threadlist[0]));
|
2014-11-28 21:46:13 +01:00
|
|
|
#if 0
|
|
|
|
memset (threadlist + nthreads, 0,
|
|
|
|
THREADLIST_CHUNK * sizeof (threadlist[0]));
|
|
|
|
#endif
|
2012-08-09 21:58:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-28 21:46:13 +01: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:
|
|
|
|
|
|
|
|
- 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE __reg3
|
|
|
|
init_cygheap::remove_tls (_cygtls *t)
|
|
|
|
{
|
|
|
|
HANDLE mutex = NULL;
|
2012-08-09 21:58:53 +02:00
|
|
|
|
2014-11-28 21:46:13 +01:00
|
|
|
tls_sentry here (INFINITE);
|
2012-08-09 21:58:53 +02:00
|
|
|
if (here.acquired ())
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
for (uint32_t i = 0; i < nthreads; i++)
|
2014-11-28 21:46:13 +01:00
|
|
|
if (t == threadlist[i].thread)
|
2012-08-09 21:58:53 +02:00
|
|
|
{
|
2014-11-28 21:46:13 +01:00
|
|
|
mutex = threadlist[i].mutex;
|
|
|
|
WaitForSingleObject (mutex, INFINITE);
|
2012-08-09 21:58:53 +02:00
|
|
|
if (i < --nthreads)
|
|
|
|
threadlist[i] = threadlist[nthreads];
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_only_printf ("removed %p element %u", this, i);
|
2012-08-09 21:58:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-28 21:46:13 +01:00
|
|
|
/* Leave with locked mutex. The calling function is responsible for
|
|
|
|
unlocking the mutex. */
|
|
|
|
return mutex;
|
2012-08-09 21:58:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-28 21:46:13 +01:00
|
|
|
threadlist_t __reg2 *
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
threadlist_t __reg3 *
|
2013-03-31 14:35:44 +02:00
|
|
|
init_cygheap::find_tls (int sig, bool& issig_wait)
|
2012-08-09 21:58:53 +02:00
|
|
|
{
|
|
|
|
debug_printf ("sig %d\n", sig);
|
|
|
|
tls_sentry here (INFINITE);
|
|
|
|
|
2014-11-28 21:46:13 +01:00
|
|
|
threadlist_t *t = NULL;
|
2013-03-31 14:35:44 +02:00
|
|
|
issig_wait = false;
|
2012-08-09 21:58:53 +02:00
|
|
|
|
2014-11-28 21:46:13 +01:00
|
|
|
int ix = -1;
|
2014-11-27 17:20:14 +01:00
|
|
|
/* Scan thread list looking for valid signal-delivery candidates */
|
|
|
|
while (++ix < (int) nthreads)
|
2012-08-09 21:58:53 +02:00
|
|
|
{
|
2014-11-28 21:46:13 +01: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-27 17:20:14 +01:00
|
|
|
{
|
2014-11-28 21:46:13 +01:00
|
|
|
t = &cygheap->threadlist[ix];
|
|
|
|
issig_wait = true;
|
|
|
|
break;
|
2014-11-27 17:20:14 +01:00
|
|
|
}
|
2014-11-28 21:46:13 +01:00
|
|
|
else if (!t && !sigismember (&(threadlist[ix].thread->sigmask), sig))
|
|
|
|
t = &cygheap->threadlist[ix];
|
2014-08-22 11:21:33 +02:00
|
|
|
}
|
2014-11-28 21:46:13 +01:00
|
|
|
/* Leave with locked mutex. The calling function is responsible for
|
|
|
|
unlocking the mutex. */
|
|
|
|
if (t)
|
|
|
|
WaitForSingleObject (t->mutex, INFINITE);
|
2012-08-09 21:58:53 +02:00
|
|
|
return t;
|
|
|
|
}
|