2004-01-14 16:45:37 +01:00
|
|
|
/* cygtls.cc
|
2003-12-23 17:43:45 +01:00
|
|
|
|
2009-01-03 06:12:22 +01:00
|
|
|
Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Red Hat, Inc.
|
2003-12-23 17:43:45 +01:00
|
|
|
|
|
|
|
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"
|
2005-07-03 04:40:30 +02:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
2003-12-23 17:43:45 +01:00
|
|
|
#include "cygtls.h"
|
|
|
|
#include <syslog.h>
|
2004-01-14 16:45:37 +01:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2004-01-19 06:46:54 +01:00
|
|
|
#include "sigproc.h"
|
2004-01-14 16:45:37 +01:00
|
|
|
|
|
|
|
class sentry
|
|
|
|
{
|
2005-04-05 06:31:00 +02:00
|
|
|
static muto lock;
|
2004-01-14 16:45:37 +01:00
|
|
|
int destroy;
|
|
|
|
public:
|
|
|
|
void init ();
|
2005-04-05 06:31:00 +02:00
|
|
|
bool acquired () {return lock.acquired ();}
|
2004-01-14 16:45:37 +01:00
|
|
|
sentry () {destroy = 0;}
|
2005-04-05 06:31:00 +02:00
|
|
|
sentry (DWORD wait) {destroy = lock.acquire (wait);}
|
|
|
|
~sentry () {if (destroy) lock.release ();}
|
2004-02-12 04:01:58 +01:00
|
|
|
friend void _cygtls::init ();
|
2004-01-14 16:45:37 +01:00
|
|
|
};
|
|
|
|
|
2005-04-05 06:31:00 +02:00
|
|
|
muto NO_COPY sentry::lock;
|
2003-12-23 17:43:45 +01:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
static size_t NO_COPY nthreads;
|
2003-12-23 17:43:45 +01:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
#define THREADLIST_CHUNK 256
|
|
|
|
|
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::init ()
|
2004-01-14 16:45:37 +01:00
|
|
|
{
|
|
|
|
if (cygheap->threadlist)
|
|
|
|
memset (cygheap->threadlist, 0, cygheap->sthreads * sizeof (cygheap->threadlist[0]));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygheap->sthreads = THREADLIST_CHUNK;
|
2007-11-26 22:30:49 +01:00
|
|
|
cygheap->threadlist = (_cygtls **) ccalloc_abort (HEAP_TLS, cygheap->sthreads,
|
|
|
|
sizeof (cygheap->threadlist[0]));
|
2004-01-14 16:45:37 +01:00
|
|
|
}
|
2005-04-05 06:31:00 +02:00
|
|
|
sentry::lock.init ("sentry_lock");
|
2004-01-14 16:45:37 +01:00
|
|
|
}
|
2003-12-23 17:43:45 +01:00
|
|
|
|
|
|
|
/* Two calls to get the stack right... */
|
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::call (DWORD (*func) (void *, void *), void *arg)
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
|
|
|
char buf[CYGTLS_PADSIZE];
|
2006-05-25 04:33:13 +02:00
|
|
|
_my_tls.call2 (func, arg, buf);
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
2006-05-25 04:33:13 +02:00
|
|
|
init_thread (buf, func);
|
2004-01-14 16:45:37 +01:00
|
|
|
DWORD res = func (arg, buf);
|
2006-05-25 04:33:13 +02:00
|
|
|
remove (INFINITE);
|
2006-05-31 04:14:17 +02:00
|
|
|
/* Don't call ExitThread on the main thread since we may have been
|
|
|
|
dynamically loaded. */
|
2006-06-02 02:09:50 +02:00
|
|
|
if ((void *) func != (void *) dll_crt0_1
|
|
|
|
&& (void *) func != (void *) dll_dllcrt0_1)
|
2006-05-31 04:14:17 +02:00
|
|
|
ExitThread (res);
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
|
|
|
if (x)
|
|
|
|
{
|
2006-05-16 05:14:24 +02:00
|
|
|
memset (this, 0, sizeof (*this));
|
2003-12-23 17:43:45 +01:00
|
|
|
stackptr = stack;
|
|
|
|
if (_GLOBAL_REENT)
|
|
|
|
{
|
|
|
|
local_clib._stdin = _GLOBAL_REENT->_stdin;
|
|
|
|
local_clib._stdout = _GLOBAL_REENT->_stdout;
|
|
|
|
local_clib._stderr = _GLOBAL_REENT->_stderr;
|
2004-09-07 06:05:14 +02:00
|
|
|
local_clib.__sdidinit = _GLOBAL_REENT->__sdidinit ? -1 : 0;
|
2003-12-23 17:43:45 +01:00
|
|
|
local_clib.__cleanup = _GLOBAL_REENT->__cleanup;
|
2004-01-26 19:52:02 +01:00
|
|
|
local_clib.__sglue._niobs = 3;
|
|
|
|
local_clib.__sglue._iobs = &_GLOBAL_REENT->__sf[0];
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
local_clib._current_locale = "C";
|
|
|
|
locals.process_logmask = LOG_UPTO (LOG_DEBUG);
|
2005-12-03 05:23:35 +01:00
|
|
|
/* Initialize this thread's ability to respond to things like
|
|
|
|
SIGSEGV or SIGFPE. */
|
|
|
|
init_exception_handler (handle_exceptions);
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
2006-02-06 19:24:11 +01:00
|
|
|
thread_id = GetCurrentThreadId ();
|
2005-12-05 21:20:18 +01:00
|
|
|
initialized = CYGTLS_INITIALIZED;
|
2003-12-23 17:43:45 +01:00
|
|
|
errno_addr = &(local_clib._errno);
|
2004-01-14 16:45:37 +01:00
|
|
|
|
|
|
|
if ((void *) func == (void *) cygthread::stub
|
|
|
|
|| (void *) func == (void *) cygthread::simplestub)
|
|
|
|
return;
|
|
|
|
|
2007-02-23 16:15:50 +01:00
|
|
|
cygheap->user.reimpersonate ();
|
2005-04-03 15:06:43 +02:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
sentry here (INFINITE);
|
|
|
|
if (nthreads >= cygheap->sthreads)
|
|
|
|
{
|
2004-02-12 04:01:58 +01:00
|
|
|
cygheap->threadlist = (_cygtls **)
|
2007-11-26 22:30:49 +01:00
|
|
|
crealloc_abort (cygheap->threadlist, (cygheap->sthreads += THREADLIST_CHUNK)
|
|
|
|
* sizeof (cygheap->threadlist[0]));
|
2004-01-14 16:45:37 +01:00
|
|
|
memset (cygheap->threadlist + nthreads, 0, THREADLIST_CHUNK * sizeof (cygheap->threadlist[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
cygheap->threadlist[nthreads++] = this;
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::fixup_after_fork ()
|
|
|
|
{
|
2004-03-16 05:39:38 +01:00
|
|
|
if (sig)
|
|
|
|
{
|
|
|
|
pop ();
|
|
|
|
sig = 0;
|
|
|
|
}
|
2005-10-24 01:47:45 +02:00
|
|
|
stacklock = spinning = 0;
|
2009-07-06 17:42:01 +02:00
|
|
|
locals.select.sockevt = NULL;
|
2004-03-12 04:09:28 +01:00
|
|
|
wq.thread_ev = NULL;
|
2004-02-12 04:01:58 +01:00
|
|
|
}
|
|
|
|
|
2005-03-16 18:07:32 +01:00
|
|
|
#define free_local(x) \
|
|
|
|
if (locals.x) \
|
|
|
|
{ \
|
|
|
|
free (locals.x); \
|
|
|
|
locals.x = NULL; \
|
|
|
|
}
|
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
void
|
|
|
|
_cygtls::remove (DWORD wait)
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
2006-03-13 05:26:57 +01:00
|
|
|
initialized = 0;
|
2009-07-06 17:42:01 +02:00
|
|
|
if (exit_state >= ES_FINAL)
|
2005-03-03 01:36:49 +01:00
|
|
|
return;
|
2006-01-05 17:26:22 +01:00
|
|
|
|
|
|
|
debug_printf ("wait %p", wait);
|
2005-04-05 19:13:35 +02:00
|
|
|
if (wait)
|
|
|
|
{
|
2005-05-11 05:33:38 +02:00
|
|
|
/* FIXME: Need some sort of atthreadexit function to allow things like
|
2005-08-12 04:39:13 +02:00
|
|
|
select to control this themselves. */
|
2009-07-07 10:07:38 +02:00
|
|
|
if (locals.select.sockevt)
|
2005-05-11 05:33:38 +02:00
|
|
|
{
|
2009-07-07 10:07:38 +02:00
|
|
|
CloseHandle (locals.select.sockevt);
|
2009-07-06 17:42:01 +02:00
|
|
|
locals.select.sockevt = NULL;
|
|
|
|
free_local (select.ser_num);
|
|
|
|
free_local (select.w4);
|
2005-05-11 05:33:38 +02:00
|
|
|
}
|
2005-04-05 19:13:35 +02:00
|
|
|
free_local (process_ident);
|
|
|
|
free_local (ntoa_buf);
|
|
|
|
free_local (protoent_buf);
|
|
|
|
free_local (servent_buf);
|
|
|
|
free_local (hostent_buf);
|
|
|
|
}
|
2005-03-16 18:07:32 +01:00
|
|
|
|
2008-03-07 12:24:51 +01:00
|
|
|
/* Free temporary TLS path buffers. */
|
|
|
|
locals.pathbufs.destroy ();
|
|
|
|
|
2004-03-15 03:47:35 +01:00
|
|
|
do
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
2004-03-15 03:47:35 +01:00
|
|
|
sentry here (wait);
|
|
|
|
if (here.acquired ())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < nthreads; i++)
|
|
|
|
if (this == cygheap->threadlist[i])
|
|
|
|
{
|
|
|
|
if (i < --nthreads)
|
|
|
|
cygheap->threadlist[i] = cygheap->threadlist[nthreads];
|
|
|
|
debug_printf ("removed %p element %d", this, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
remove_wq (wait);
|
2003-12-23 17:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-12-05 21:20:18 +01:00
|
|
|
_cygtls::push (__stack_t addr)
|
2003-12-23 17:43:45 +01:00
|
|
|
{
|
|
|
|
*stackptr++ = (__stack_t) addr;
|
|
|
|
}
|
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls *
|
|
|
|
_cygtls::find_tls (int sig)
|
2004-01-14 16:45:37 +01:00
|
|
|
{
|
2008-03-01 15:53:44 +01:00
|
|
|
static int NO_COPY threadlist_ix;
|
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
debug_printf ("sig %d\n", sig);
|
|
|
|
sentry here (INFINITE);
|
2008-03-01 15:53:44 +01:00
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls *res = NULL;
|
2008-03-01 15:53:44 +01:00
|
|
|
threadlist_ix = -1;
|
|
|
|
|
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted ())
|
|
|
|
cygheap->threadlist[threadlist_ix]->remove (INFINITE);
|
|
|
|
|
|
|
|
while (++threadlist_ix < (int) nthreads)
|
2004-01-14 16:45:37 +01:00
|
|
|
if (sigismember (&(cygheap->threadlist[threadlist_ix]->sigwait_mask), sig))
|
|
|
|
{
|
|
|
|
res = cygheap->threadlist[threadlist_ix];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-19 06:46:54 +01:00
|
|
|
void
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::set_siginfo (sigpacket *pack)
|
2004-01-19 06:46:54 +01:00
|
|
|
{
|
|
|
|
infodata = pack->si;
|
|
|
|
}
|
|
|
|
|
2006-05-25 04:33:13 +02:00
|
|
|
/* Set up the exception handler for the current thread. The x86 uses segment
|
2005-12-03 05:23:35 +01:00
|
|
|
register fs, offset 0 to point to the current exception handler. */
|
|
|
|
|
|
|
|
extern exception_list *_except_list asm ("%fs:0");
|
|
|
|
|
|
|
|
void
|
|
|
|
_cygtls::init_exception_handler (exception_handler *eh)
|
|
|
|
{
|
2009-07-17 20:17:11 +02:00
|
|
|
/* Here in the distant past of 17-Jul-2009, we had an issue where Windows
|
|
|
|
2008 became YA perplexed because the cygwin exception handler was added
|
|
|
|
at the start of the SEH while still being in the list further on. This
|
|
|
|
was because we added a loop by setting el.prev to _except_list here.
|
|
|
|
Since el is reused in this thread, and this function can be called
|
|
|
|
more than once when a dll is loaded, this is not a good thing.
|
|
|
|
|
|
|
|
So, for now, until the next required tweak, we will just avoid adding the
|
|
|
|
cygwin exception handler if it is already on this list. This could present
|
|
|
|
a problem if some previous exception handler tries to do things that are
|
|
|
|
better left to Cygwin. I await the cygwin mailing list notification of
|
|
|
|
this event with bated breath.
|
|
|
|
|
|
|
|
(cgf 2009-07-17) */
|
|
|
|
for (exception_list *e = _except_list;
|
|
|
|
e != NULL && e != (exception_list *) -1;
|
|
|
|
e = e->prev)
|
2009-07-17 18:54:21 +02:00
|
|
|
if (e == &el)
|
|
|
|
return;
|
2005-12-03 05:23:35 +01:00
|
|
|
el.handler = eh;
|
2008-02-13 10:42:22 +01:00
|
|
|
/* Apparently Windows stores some information about an exception and tries
|
|
|
|
to figure out if the SEH which returned 0 last time actually solved the
|
|
|
|
problem, or if the problem still persists (e.g. same exception at same
|
|
|
|
address). In this case Windows seems to decide that it can't trust
|
|
|
|
that SEH and calls the next handler in the chain instead.
|
|
|
|
|
|
|
|
At one point this was a loop (el.prev = ⪙). This outsmarted the
|
|
|
|
above behaviour. Unfortunately this trick doesn't work anymore with
|
|
|
|
Windows 2008, which irremediably gets into an endless loop, taking 100%
|
2008-02-27 18:57:14 +01:00
|
|
|
CPU. That's why we reverted to a normal SEH chain and changed the way
|
|
|
|
the exception handler returns to the application. */
|
2008-03-12 13:41:50 +01:00
|
|
|
el.prev = _except_list;
|
2005-12-03 05:23:35 +01:00
|
|
|
_except_list = ⪙
|
|
|
|
}
|