summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/dcrt0.cc
diff options
context:
space:
mode:
authorChristopher Faylor <me@cgf.cx>2004-01-14 15:45:37 +0000
committerChristopher Faylor <me@cgf.cx>2004-01-14 15:45:37 +0000
commit2d1d1eb1e4cc7090a406a48da7a896b2dbd5d140 (patch)
tree859e69d9c8bcf2cae2bd112a54201c42ce2be4a4 /winsup/cygwin/dcrt0.cc
parentd8589584263e12f305906457edfaa5051ee569a4 (diff)
downloadcygnal-2d1d1eb1e4cc7090a406a48da7a896b2dbd5d140.tar.gz
cygnal-2d1d1eb1e4cc7090a406a48da7a896b2dbd5d140.tar.bz2
cygnal-2d1d1eb1e4cc7090a406a48da7a896b2dbd5d140.zip
* cygtls.h (_threadinfo::call): Remove regparm declaration to work around
compiler bug. * autoload.cc (TryEnterCriticalSection): Remove. * dcrt0.cc (dll_crt0_0): Delete inappropriate setting of _my_tls.stackptr to NULL since it has really bad consequences. Make 'si' an automatic variable. * cygtls.cc (_threadinfo::init_thread): Correct thinko which caused thread list to be allocated every time. * cygtls.h (CYGTLS_PADSIZE): Define as const int. * sync.h: Make multiple inclusion safe. (muto::next): Eliminate. (muto::exiting_thread): New variable. (muto::set_exiting_thread): New function. (new_muto): Change to use different section for mutos since c++ give inexplicable warning in some cases otherwise. (new_muto1): Ditto. * dcrt0.cc (do_exit): Call muto::set_exiting_thread here. * sync.cc (muto_start): Eliminate. (muto::acquire): Always give exiting thread a lock. Never give thread a lock if exiting. (muto::release): Ditto for releasing. * dtable.cc (dtable::init_lock): Unline function and define here. * dtable.h (lock_cs): Define as a muto since critical sections seem to work oddly on Windows Me. (lock): Accommodate switch to muto. (unlock): Ditto. * exceptions.cc (setup_handler): Don't worry about acquiring mutos since that hasn't mattered for a long time. (signal_exit): Ditto: muto stuff will be handled automatically on exit now. * Makefile.in (DLL_IMPORTS): Link advapi32 to ensure proper DLL initialization. * autoload.cc (RegCloseKey): Arbitrarily choose this function as a "seed" to pull the advapi32 link library in. So, comment out the autoloading. * cygtls.cc (_threadinfo::init_thread): Just clear CYGTLS_PADSIZE. (_threadinfo::remove): Add debugging. (_threadinfo::find_tls): Ditto. * cygtls.h (_threadinfo::padding): Make zero length (for now?). * dcrt0.cc (dll_crt0_0): Move more initialization here from dll_crt0_1. (dll_crt0_1): See above. * dtable.h (dtable::lock): Remove commented out critical section locking. * dtable.h (dtable::init_lock): Remove commented out critical section locking. * dtable.h (dtable::unlock): Remove commented out critical section locking. * exceptions.cc (interruptible): bool'ize. * init.cc (threadfunc_fe): Revert to storing threadfunc at stack bottom. (munge_threadfunc): Ditto. Avoid adding overhead to calibration_thread. (prime_threads): Don't initialize tls stuff. (dll_entry): Make minor change to initialization order. * tlsoffsets.h: Regenerate. * sigproc.cc (wait_sig): Add sanity check for end of process thread exit. * select.h: Make minor formatting change. * Makefile.in: Add still more -fomit-frame-pointer functions. * dtable.h (dtable::lock): New function. (dtable::unlock): New function. (dtable::init_lock): New function. * cygheap.h (HEAP_TLS): Declare new enum value. (init_cygheap::threadlist): Declare new array. (init_cygheap::sthreads): Declare new variable. (cygheap_fdmanip::~cygheap_fdmanip): Use new dtable lock/unlock functions. (cygheap_fdnew::cygheap_fdnew): Ditto. (cygheap_fdget::cygheap_fdget): Ditto. * dtable.cc (dtable_init): Initialize fdtab critical section. (dtable::fixup_after_fork): Ditto. (dtable::fixup_after_exec): Ditto. (dtable::dup2): Use lock/unlock calls to protect access to fdtab. (dtable::find_fifo): Ditto. (dtable::fixup_before_fork): Ditto. (dtable::fixup_before_exec): Ditto. (dtable::set_file_pointers_for_exec): Ditto. (dtable::vfork_child_dup): Ditto. (dtable::vfork_parent_restore): Ditto. * syscalls.cc (close_all_files): Ditto. * sync.h (muto::acquired): Declare new function. (new_muto1): Declare new macro used to specify name of muto storage. * sync.cc (muto::acquired): Define new function. * cygthread.cc (cygthread::stub): Remove signal chain removal call since it is handled during initialization now. * cygthread.cc (cygthread::simplestub): Remove signal chain removal call since it is handled during initialization now. * cygtls.cc (sentry): New class used for locking. Use throughout. (_threadinfo::reset_exception): Don't pop stack. (_threadinfo::find_tls): Move from exceptions.cc. (_threadinfo::init_thread): Initialize array of threads rather than linked list. Take second argument indicating thread function for this thread. (_threadinfo::remove): Search thread array rather than linked list. Use sentry to lock. Only unlock if we got the lock. (_threadinfo::find_tls): Ditto for first two. (handle_threadlist_exception): Handle exceptions when manipulating the thread list in case of premature thread termination. (_threadinfo::init_threadlist_exceptions): Ditto. * cygtls.h (TLS_STACK_SIZE): Decrease size. (_threadinfo::padding): Add element to avoid overwriting lower part of stack. (_threadinfo::remove): Add a "wait" argument to control how long we wait for a lock before removing. * exceptions.cc (init_exception_handler): Make global. Take argument to control exception handler being set. (ctrl_c_handler): Wait forever when removing self from signal chain. (_threadinfo::find_tls): Move to cygtls.cc. (sig_handle): Reorganize detection for thread-specific signals. * heap.cc (heap_init): Rework slightly. Make fatal error more verbose. Remove malloc initialization since it can't happen during dll attach. * init.cc (search_for): Move address to search for on stack here. (threadfunc_ix): Ditto for stack offset. Make shared so that stack walk potentially only has to be done once when cygwin processes are running. (threadfunc_fe): Use standard tls to store thread function (may change back later). (calibration_thread): New function. Potentially called to find threadfunc_ix. (munge_threadfunc): Search for "search_for" value on stack. Output warning when thread func not found on stack. Use standard tls to store thread function. (prime_threads): New function. Called to prime thread front end. (dll_entry): Call dll_crt0_0 here when DLL_PROCESS_ATTACH. Call prime_threads here. Try to remove thread from signal list here. * sigproc.cc (wait_sig): Initialize threadlist exception stuff here. * thread.cc (pthread::exit): Pass argument to signal list remove function. * thread.h: Remove obsolete *ResourceLock defines. * tlsoffsets.h: Regenerate. * winsup.h (spf): Define temporary debug macro to be deleted later. * dcrt0.cc (dll_crt0_0): New function, called during DLL initialization. Mainly consists of code pulled from dll_crt0_1. (dll_crt0_1): See above. (_dll_crt0): Wait for initial calibration thread to complete, if appropriate. Move some stuff to dll_crt0_0. (initialize_main_tls): Accommodate argument change to _thread_info::init_thread. * fork.cc (fork_child): Ditto. (sync_with_child): Fix debug message. * external.cc (cygwin_internal): Remove special considerations for uninitialized dll since initialization happens during dll attach now. * dlfcn.cc (dlopen): Remove obsolete *ResourceLock calls. (dlclose): Ditto. * cygheap.h (init_cygheap::close_ctty): Declare new function. * cygheap.cc (init_cygheap::close_ctty): Define new function. * syscalls.cc (close_all_files): Use close_ctty. (setsid): Ditto. * cygthread.cc (cygthread::stub): Remove exception initialization. * cygthread.cc (cygthread::stub): Remove exception initialization. (cygthread::simplestub): Ditto. * thread.cc (pthread::thread_init_wrapper): Ditto. * cygtls.cc (_last_thread): Make static. (_threadinfo::call2): Initialize exception handler here. (_threadinfo::find_tls): Move here. * exceptions.cc (_threadinfo::find_tls): Move. * dcrt0.cc (__api_fatal): Add prefix info to message here rather than including it in every call to function. * winsup.h (api_fatal): Accommodate above change. * debug.cc (add_handle): Don't do anything if cygheap not around. (mark_closed): Ditto. * dll_init.cc (dll_list::detach): Fix debug output. * fork.cc (sync_with_child): Ditto. (vfork): Improve debug output. * heap.cc (heap_init): Ditto. * exceptions.cc (try_to_debug): Clarify message when debugger attaches.
Diffstat (limited to 'winsup/cygwin/dcrt0.cc')
-rw-r--r--winsup/cygwin/dcrt0.cc317
1 files changed, 169 insertions, 148 deletions
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index f97c0f927..3d6b7a89b 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -35,6 +35,7 @@ details. */
#include "dll_init.h"
#include "cygthread.h"
#include "sync.h"
+#include "heap.h"
#define MAX_AT_FILE_LEVEL 10
@@ -55,11 +56,13 @@ bool strip_title_path;
bool allow_glob = true;
codepage_type current_codepage = ansi_cp;
-static NO_COPY int mypid = 0;
int __argc_safe;
int _declspec(dllexport) __argc;
char _declspec(dllexport) **__argv;
-vfork_save NO_COPY *main_vfork = NULL;
+vfork_save NO_COPY *main_vfork;
+
+static int NO_COPY envc;
+char NO_COPY **envp;
extern "C" void __sinit (_reent *);
@@ -519,41 +522,127 @@ alloc_stack (child_info_fork *ci)
return;
}
-/* Take over from libc's crt0.o and start the application. Note the
- various special cases when Cygwin DLL is being runtime loaded (as
- opposed to being link-time loaded by Cygwin apps) from a non
- cygwin app via LoadLibrary. */
+#ifdef DEBUGGING
+void
+break_here ()
+{
+ debug_printf ("break here");
+}
+#endif
+
static void
-dll_crt0_1 (char *)
+initial_env ()
{
- /* According to onno@stack.urc.tue.nl, the exception handler record must
- be on the stack. */
- /* FIXME: Verify forked children get their exception handler set up ok. */
- exception_list cygwin_except_entry;
+ char buf[CYG_MAX_PATH + 1];
+#ifdef DEBUGGING
+ DWORD len;
+ if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
+ {
+ DWORD ms = atoi (buf);
+ buf[0] = '\0';
+ len = GetModuleFileName (NULL, buf, CYG_MAX_PATH);
+ console_printf ("Sleeping %d, pid %u %s\n", ms, GetCurrentProcessId (), buf);
+ while (ms--)
+ Sleep (1);
+ }
+ if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
+ {
+ char buf1[CYG_MAX_PATH + 1];
+ len = GetModuleFileName (NULL, buf1, CYG_MAX_PATH);
+ strlwr (buf1);
+ strlwr (buf);
+ char *p = strchr (buf, ':');
+ if (!p)
+ p = (char *) "gdb.exe -nw";
+ else
+ *p++ = '\0';
+ if (strstr (buf1, buf))
+ {
+ error_start_init (p);
+ try_to_debug ();
+ break_here ();
+ }
+ }
+#endif
- /* Initialize SIGSEGV handling, etc. */
- init_exceptions (&cygwin_except_entry);
+ if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
+ _cygwin_testing = 1;
+}
- /* Set the os_being_run global. */
+void __stdcall
+dll_crt0_0 ()
+{
wincap.init ();
- device::init ();
- check_sanity_and_sync (user_data);
+ initial_env ();
- do_global_ctors (&__CTOR_LIST__, 1);
+ char zeros[sizeof (child_proc_info->zero)] = {0};
- /* Nasty static stuff needed by newlib -- point to a local copy of
- the reent stuff.
- Note: this MUST be done here (before the forkee code) as the
- fork copy code doesn't copy the data in libccrt0.cc (that's why we
- pass in the per_process struct into the .dll from libccrt0). */
+ init_console_handler ();
+ init_global_security ();
+ if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
+ GetCurrentProcess (), &hMainProc, 0, FALSE,
+ DUPLICATE_SAME_ACCESS))
+ hMainProc = GetCurrentProcess ();
- user_data->resourcelocks->Init ();
- user_data->threadinterface->Init ();
+ DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
+ &hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
- winpids::init ();
+ (void) SetErrorMode (SEM_FAILCRITICALERRORS);
- int envc = 0;
- char **envp = NULL;
+ STARTUPINFO si;
+ GetStartupInfo (&si);
+ child_proc_info = (child_info *) si.lpReserved2;
+
+ int mypid = 0;
+ if (si.cbReserved2 < EXEC_MAGIC_SIZE || !child_proc_info
+ || memcmp (child_proc_info->zero, zeros,
+ sizeof (child_proc_info->zero)) != 0)
+ child_proc_info = NULL;
+ else
+ {
+ if ((child_proc_info->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
+ multiple_cygwin_problem ("proc", child_proc_info->intro, 0);
+ else if (child_proc_info->intro == PROC_MAGIC_GENERIC
+ && child_proc_info->magic != CHILD_INFO_MAGIC)
+ multiple_cygwin_problem ("proc", child_proc_info->magic,
+ CHILD_INFO_MAGIC);
+ else if (child_proc_info->cygheap != (void *) &_cygheap_start)
+ multiple_cygwin_problem ("cygheap", (DWORD) child_proc_info->cygheap,
+ (DWORD) &_cygheap_start);
+ unsigned should_be_cb = 0;
+ switch (child_proc_info->type)
+ {
+ case _PROC_FORK:
+ user_data->forkee = child_proc_info->cygpid;
+ should_be_cb = sizeof (child_info_fork);
+ /* fall through */;
+ case _PROC_SPAWN:
+ case _PROC_EXEC:
+ if (!should_be_cb)
+ should_be_cb = sizeof (child_info);
+ if (should_be_cb != child_proc_info->cb)
+ multiple_cygwin_problem ("proc size", child_proc_info->cb, should_be_cb);
+ else if (sizeof (fhandler_union) != child_proc_info->fhandler_union_cb)
+ multiple_cygwin_problem ("fhandler size", child_proc_info->fhandler_union_cb, sizeof (fhandler_union));
+ else
+ {
+ cygwin_user_h = child_proc_info->user_h;
+ mypid = child_proc_info->cygpid;
+ break;
+ }
+ default:
+ system_printf ("unknown exec type %d", child_proc_info->type);
+ /* intentionally fall through */
+ case _PROC_WHOOPS:
+ child_proc_info = NULL;
+ break;
+ }
+ }
+
+ device::init ();
+ winpids::init ();
+ do_global_ctors (&__CTOR_LIST__, 1);
+ cygthread::init ();
if (!child_proc_info)
memory_init ();
@@ -591,6 +680,7 @@ dll_crt0_1 (char *)
__argv = spawn_info->moreinfo->argv;
envp = spawn_info->moreinfo->envp;
envc = spawn_info->moreinfo->envc;
+ envp = spawn_info->moreinfo->envp;
cygheap->fdtab.fixup_after_exec (spawn_info->parent);
signal_fixup_after_exec ();
CloseHandle (spawn_info->parent);
@@ -607,9 +697,44 @@ dll_crt0_1 (char *)
CloseHandle (child_proc_info->pppid_handle);
}
+ _threadinfo::init ();
+
+ /* Initialize events */
+ events_init ();
+
+ /* Init global well known SID objects */
+ cygsid::init ();
+ cygheap->cwd.init ();
+}
+
+/* Take over from libc's crt0.o and start the application. Note the
+ various special cases when Cygwin DLL is being runtime loaded (as
+ opposed to being link-time loaded by Cygwin apps) from a non
+ cygwin app via LoadLibrary. */
+static void
+dll_crt0_1 (char *)
+{
+ /* According to onno@stack.urc.tue.nl, the exception handler record must
+ be on the stack. */
+ /* FIXME: Verify forked children get their exception handler set up ok. */
+ exception_list cygwin_except_entry;
+
+ check_sanity_and_sync (user_data);
+ malloc_init ();
+
+ /* Initialize SIGSEGV handling, etc. */
+ init_exceptions (&cygwin_except_entry);
+
+ /* Nasty static stuff needed by newlib -- point to a local copy of
+ the reent stuff.
+ Note: this MUST be done here (before the forkee code) as the
+ fork copy code doesn't copy the data in libccrt0.cc (that's why we
+ pass in the per_process struct into the .dll from libccrt0). */
+
+ user_data->resourcelocks->Init ();
+ user_data->threadinterface->Init ();
ProtectHandle (hMainProc);
ProtectHandle (hMainThread);
- cygthread::init ();
/* Initialize pthread mainthread when not forked and it is safe to call new,
otherwise it is reinitalized in fixup_after_fork */
@@ -629,12 +754,6 @@ dll_crt0_1 (char *)
cygheap->fdtab.vfork_child_fixup ();
- (void) SetErrorMode (SEM_FAILCRITICALERRORS);
-
- /* Initialize events. */
- events_init ();
-
- cygheap->cwd.init ();
main_vfork = vfork_storage.create ();
cygbench ("pre-forkee");
@@ -663,9 +782,6 @@ dll_crt0_1 (char *)
}
#endif
- /* Init global well known SID objects */
- cygsid::init ();
-
/* Initialize our process table entry. */
pinfo_init (envp, envc);
@@ -773,60 +889,13 @@ dll_crt0_1 (char *)
exit (user_data->main (__argc, __argv, *user_data->envptr));
}
-#ifdef DEBUGGING
-void
-break_here ()
-{
- debug_printf ("break here");
-}
-#endif
-
-void
-initial_env ()
-{
- char buf[CYG_MAX_PATH + 1];
-#ifdef DEBUGGING
- DWORD len;
- if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
- {
- DWORD ms = atoi (buf);
- buf[0] = '\0';
- len = GetModuleFileName (NULL, buf, CYG_MAX_PATH);
- console_printf ("Sleeping %d, pid %u %s\n", ms, GetCurrentProcessId (), buf);
- Sleep (ms);
- }
- if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
- {
- char buf1[CYG_MAX_PATH + 1];
- len = GetModuleFileName (NULL, buf1, CYG_MAX_PATH);
- strlwr (buf1);
- strlwr (buf);
- char *p = strchr (buf, ':');
- if (!p)
- p = (char *) "gdb.exe -nw";
- else
- *p++ = '\0';
- if (strstr (buf1, buf))
- {
- error_start_init (p);
- try_to_debug ();
- break_here ();
- }
- }
-#endif
-
- if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
- _cygwin_testing = 1;
-}
-
struct _reent *
initialize_main_tls (char *padding)
{
if (!_main_tls)
{
- _threadinfo::init ();
_main_tls = &_my_tls;
- _main_tls->init_thread (padding);
+ _main_tls->init_thread (padding, NULL);
}
return &_main_tls->local_clib;
}
@@ -836,81 +905,31 @@ initialize_main_tls (char *padding)
UPTR is a pointer to global data that lives on the libc side of the
line [if one distinguishes the application from the dll]. */
-
+
extern "C" void __stdcall
_dll_crt0 ()
{
- initial_env ();
- char zeros[max (CYGTLS_PADSIZE, sizeof (child_proc_info->zero))] = {0};
- static NO_COPY STARTUPINFO si;
+ extern HANDLE sync_startup;
+ if (sync_startup)
+ {
+ (void) WaitForSingleObject (sync_startup, INFINITE);
+ CloseHandle (sync_startup);
+ }
main_environ = user_data->envptr;
*main_environ = NULL;
- init_console_handler ();
- init_global_security ();
- if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
- GetCurrentProcess (), &hMainProc, 0, FALSE,
- DUPLICATE_SAME_ACCESS))
- hMainProc = GetCurrentProcess ();
+ if (child_proc_info && child_proc_info->type == _PROC_FORK)
+ user_data->forkee = child_proc_info->cygpid;
- DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
- &hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
-
- GetStartupInfo (&si);
- child_proc_info = (child_info *) si.lpReserved2;
- if (si.cbReserved2 < EXEC_MAGIC_SIZE || !child_proc_info
- || memcmp (child_proc_info->zero, zeros,
- sizeof (child_proc_info->zero)) != 0)
- child_proc_info = NULL;
- else
- {
- if ((child_proc_info->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
- multiple_cygwin_problem ("proc", child_proc_info->intro, 0);
- else if (child_proc_info->intro == PROC_MAGIC_GENERIC
- && child_proc_info->magic != CHILD_INFO_MAGIC)
- multiple_cygwin_problem ("proc", child_proc_info->magic,
- CHILD_INFO_MAGIC);
- else if (child_proc_info->cygheap != (void *) &_cygheap_start)
- multiple_cygwin_problem ("cygheap", (DWORD) child_proc_info->cygheap,
- (DWORD) &_cygheap_start);
- unsigned should_be_cb = 0;
- switch (child_proc_info->type)
- {
- case _PROC_FORK:
- user_data->forkee = child_proc_info->cygpid;
- should_be_cb = sizeof (child_info_fork);
- /* fall through */;
- case _PROC_SPAWN:
- case _PROC_EXEC:
- if (!should_be_cb)
- should_be_cb = sizeof (child_info);
- if (should_be_cb != child_proc_info->cb)
- multiple_cygwin_problem ("proc size", child_proc_info->cb, should_be_cb);
- else if (sizeof (fhandler_union) != child_proc_info->fhandler_union_cb)
- multiple_cygwin_problem ("fhandler size", child_proc_info->fhandler_union_cb, sizeof (fhandler_union));
- else
- {
- cygwin_user_h = child_proc_info->user_h;
- mypid = child_proc_info->cygpid;
- break;
- }
- default:
- system_printf ("unknown exec type %d", child_proc_info->type);
- /* intentionally fall through */
- case _PROC_WHOOPS:
- child_proc_info = NULL;
- break;
- }
- }
-
+ char padding[CYGTLS_PADSIZE];
_impure_ptr = &reent_data;
_impure_ptr->_stdin = &_impure_ptr->__sf[0];
_impure_ptr->_stdout = &_impure_ptr->__sf[1];
_impure_ptr->_stderr = &_impure_ptr->__sf[2];
_impure_ptr->_current_locale = "C";
- initialize_main_tls (zeros);
- dll_crt0_1 (zeros);
+ initialize_main_tls (padding);
+ dll_crt0_1 (padding);
}
void
@@ -970,6 +989,7 @@ do_exit (int status)
}
EnterCriticalSection (&exit_lock);
+ muto::set_exiting_thread ();
if (exit_state < ES_EVENTS_TERMINATE)
{
exit_state = ES_EVENTS_TERMINATE;
@@ -1090,7 +1110,8 @@ __api_fatal (const char *fmt, ...)
va_list ap;
va_start (ap, fmt);
- __small_vsprintf (buf, fmt, ap);
+ int n = __small_sprintf (buf, "%P (%u): *** ", cygwin_pid (GetCurrentProcessId ()));
+ __small_vsprintf (buf + n, fmt, ap);
va_end (ap);
strcat (buf, "\n");
int len = strlen (buf);