1 /* dcrt0.cc -- essentially the main() for the Cygwin dll
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
17 #include "exceptions.h"
26 #include "perprocess.h"
32 #include "child_info_magic.h"
34 #include "shared_info.h"
35 #include "cygwin_version.h"
41 #define MAX_AT_FILE_LEVEL 10
43 #define PREMAIN_LEN (sizeof (user_data->premain) / sizeof (user_data->premain[0]))
45 extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
47 HANDLE NO_COPY hMainProc = (HANDLE) -1;
48 HANDLE NO_COPY hMainThread;
49 HANDLE NO_COPY hProcToken;
50 HANDLE NO_COPY hProcImpToken;
52 muto NO_COPY lock_process::locker;
55 bool strip_title_path;
56 bool allow_glob = true;
57 bool NO_COPY in_forkee;
58 codepage_type current_codepage = ansi_cp;
61 int _declspec(dllexport) __argc;
62 char _declspec(dllexport) **__argv;
64 vfork_save NO_COPY *main_vfork;
67 static int NO_COPY envc;
70 extern "C" void __sinit (_reent *);
72 _cygtls NO_COPY *_main_tls;
74 bool NO_COPY cygwin_finished_initializing;
76 /* Used in SIGTOMASK for generating a bit for insertion into a sigset_t.
77 This is subtracted from the signal number prior to shifting the bit.
78 In older versions of cygwin, the signal was used as-is to shift the
79 bit for masking. So, we'll temporarily detect this and set it to zero
80 for programs that are linked using older cygwins. This is just a stopgap
81 measure to allow an orderly transfer to the new, correct sigmask method. */
82 unsigned NO_COPY int signal_shift_subtract = 1;
84 ResourceLocks _reslock NO_COPY;
85 MTinterface _mtinterf;
87 bool NO_COPY _cygwin_testing;
89 char NO_COPY almost_null[1];
93 /* This is an exported copy of environ which can be used by DLLs
94 which use cygwin.dll. */
95 char **__cygwin_environ;
96 char ***main_environ = &__cygwin_environ;
97 /* __progname used in getopt error message */
99 struct per_process __cygwin_user_data =
100 {/* initial_sp */ 0, /* magic_biscuit */ 0,
101 /* dll_major */ CYGWIN_VERSION_DLL_MAJOR,
102 /* dll_major */ CYGWIN_VERSION_DLL_MINOR,
103 /* impure_ptr_ptr */ NULL, /* envptr */ NULL,
104 /* malloc */ malloc, /* free */ free,
105 /* realloc */ realloc,
106 /* fmode_ptr */ NULL, /* main */ NULL, /* ctors */ NULL,
107 /* dtors */ NULL, /* data_start */ NULL, /* data_end */ NULL,
108 /* bss_start */ NULL, /* bss_end */ NULL,
110 /* premain */ {NULL, NULL, NULL, NULL},
112 /* unused */ {0, 0, 0, 0, 0, 0, 0},
113 /* UNUSED forkee */ 0,
115 /* api_major */ CYGWIN_VERSION_API_MAJOR,
116 /* api_minor */ CYGWIN_VERSION_API_MINOR,
117 /* unused2 */ {0, 0, 0, 0, 0},
118 /* resourcelocks */ &_reslock, /* threadinterface */ &_mtinterf,
119 /* impure_ptr */ _GLOBAL_REENT,
121 bool ignore_case_with_glob;
122 int __declspec (dllexport) _check_for_executable = true;
126 int NO_COPY __api_fatal_exit_val = 1;
130 char title_buf[TITLESIZE + 1];
135 void (**pfunc) () = user_data->dtors;
138 user_data->dtors = NULL;
144 static void __stdcall
145 do_global_ctors (void (**in_pfunc)(), int force)
147 if (!force && in_forkee)
148 return; // inherit constructed stuff from parent pid
150 /* Run ctors backwards, so skip the first entry and find how many
151 there are, then run them. */
153 void (**pfunc) () = in_pfunc;
157 while (--pfunc > in_pfunc)
162 * Replaces @file in the command line with the contents of the file.
163 * There may be multiple @file's in a single command line
164 * A \@file is replaced with @file so that echo \@foo would print
165 * @foo and not the contents of foo.
167 static bool __stdcall
168 insert_file (char *name, char *&cmd)
173 f = CreateFile (name + 1,
174 GENERIC_READ, /* open for reading */
175 FILE_SHARE_READ, /* share for reading */
176 &sec_none_nih, /* no security */
177 OPEN_EXISTING, /* existing file only */
178 FILE_ATTRIBUTE_NORMAL, /* normal file */
179 NULL); /* no attr. template */
181 if (f == INVALID_HANDLE_VALUE)
183 debug_printf ("couldn't open file '%s', %E", name);
187 /* This only supports files up to about 4 billion bytes in
188 size. I am making the bold assumption that this is big
189 enough for this feature */
190 size = GetFileSize (f, NULL);
191 if (size == 0xFFFFFFFF)
193 debug_printf ("couldn't get file size for '%s', %E", name);
197 int new_size = strlen (cmd) + size + 2;
198 char *tmp = (char *) malloc (new_size);
201 debug_printf ("malloc failed, %E");
205 /* realloc passed as it should */
208 rf_result = ReadFile (f, tmp, size, &rf_read, NULL);
210 if (!rf_result || (rf_read != size))
212 debug_printf ("ReadFile failed, %E");
217 strcpy (tmp + size, cmd);
226 return ch == '"' || ch == '\'';
229 /* Step over a run of characters delimited by quotes */
230 static /*__inline*/ char *
231 quoted (char *cmd, int winshell)
239 strcpy (cmd, cmd + 1);
240 if (*(p = strechr (cmd, quote)))
245 const char *s = quote == '\'' ? "'" : "\\\"";
246 /* This must have been run from a Windows shell, so preserve
247 quotes for globify to play with later. */
248 while (*cmd && *++cmd)
249 if ((p = strpbrk (cmd, s)) == NULL)
251 cmd = strchr (cmd, '\0'); // no closing quote
256 else if (quote == '"' && p[1] == '"')
259 cmd = ++p; // a quoted quote
263 cmd = p + 1; // point to after end
269 /* Perform a glob on word if it contains wildcard characters.
270 Also quote every character between quotes to force glob to
271 treat the characters literally. */
273 globify (char *word, char **&argv, int &argc, int &argvlen)
275 if (*word != '~' && strpbrk (word, "?*[\"\'(){}") == NULL)
280 int dos_spec = isdrive (word);
281 if (!dos_spec && isquote (*word) && word[1] && word[2])
282 dos_spec = isdrive (word + 1);
284 /* We'll need more space if there are quoting characters in
285 word. If that is the case, doubling the size of the
286 string should provide more than enough space. */
287 if (strpbrk (word, "'\""))
289 char pattern[strlen (word) + ((dos_spec + 1) * n) + 1];
291 /* Fill pattern with characters from word, quoting any
292 characters found within quotes. */
293 for (p = pattern, s = word; *s != '\000'; s++, p++)
296 if (dos_spec && *s == '\\')
303 while (*++s && *s != quote)
305 if (dos_spec || *s != '\\')
307 else if (s[1] == quote || s[1] == '\\')
323 /* Attempt to match the argument. Return just word (minus quoting) if no match. */
324 if (glob (pattern, GLOB_TILDE | GLOB_NOCHECK | GLOB_BRACE | GLOB_QUOTE, NULL, &gl) || !gl.gl_pathc)
327 /* Allocate enough space in argv for the matched filenames. */
329 if ((argc += gl.gl_pathc) > argvlen)
332 argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
335 /* Copy the matched filenames to argv. */
336 char **gv = gl.gl_pathv;
337 char **av = argv + n;
340 debug_printf ("argv[%d] = '%s'", n++, *gv);
344 /* Clean up after glob. */
349 /* Build argv, argc from string passed from Windows. */
351 static void __stdcall
352 build_argv (char *cmd, char **&argv, int &argc, int winshell)
355 int nesting = 0; // monitor "nesting" from insert_file
361 /* Scan command line until there is nothing left. */
371 /* Found the beginning of an argument. */
373 char *sawquote = NULL;
376 if (*cmd != '"' && (!winshell || *cmd != '\''))
377 cmd++; // Skip over this character
379 /* Skip over characters until the closing quote */
382 cmd = quoted (cmd, winshell && argc > 0);
384 if (issep (*cmd)) // End of argument if space
388 *cmd++ = '\0'; // Terminate `word'
390 /* Possibly look for @file construction assuming that this isn't
391 the very first argument and the @ wasn't quoted */
392 if (argc && sawquote != word && *word == '@')
394 if (++nesting > MAX_AT_FILE_LEVEL)
395 api_fatal ("Too many levels of nesting for %s", word);
396 if (insert_file (word, cmd))
397 continue; // There's new stuff in cmd now
400 /* See if we need to allocate more space for argv */
404 argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
407 /* Add word to argv file after (optional) wildcard expansion. */
408 if (!winshell || !argc || !globify (word, argv, argc, argvlen))
410 debug_printf ("argv[%d] = '%s'", argc, word);
417 debug_printf ("argc %d", argc);
420 /* sanity and sync check */
422 check_sanity_and_sync (per_process *p)
424 /* Sanity check to make sure developers didn't change the per_process */
425 /* struct without updating SIZEOF_PER_PROCESS [it makes them think twice */
426 /* about changing it]. */
427 if (sizeof (per_process) != SIZEOF_PER_PROCESS)
429 api_fatal ("per_process sanity check failed");
432 /* Make sure that the app and the dll are in sync. */
434 /* Complain if older than last incompatible change */
435 if (p->dll_major < CYGWIN_VERSION_DLL_EPOCH)
436 api_fatal ("cygwin DLL and APP are out of sync -- DLL version mismatch %d < %d",
437 p->dll_major, CYGWIN_VERSION_DLL_EPOCH);
439 /* magic_biscuit != 0 if using the old style version numbering scheme. */
440 if (p->magic_biscuit != SIZEOF_PER_PROCESS)
441 api_fatal ("Incompatible cygwin .dll -- incompatible per_process info %d != %d",
442 p->magic_biscuit, SIZEOF_PER_PROCESS);
444 /* Complain if incompatible API changes made */
445 if (p->api_major > cygwin_version.api_major)
446 api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %d > %d",
447 p->api_major, cygwin_version.api_major);
449 if (CYGWIN_VERSION_DLL_MAKE_COMBINED (p->dll_major, p->dll_minor) <=
450 CYGWIN_VERSION_DLL_BAD_SIGNAL_MASK)
451 signal_shift_subtract = 0;
454 child_info NO_COPY *child_proc_info = NULL;
455 static MEMORY_BASIC_INFORMATION NO_COPY sm;
457 #define CYGWIN_GUARD ((wincap.has_page_guard ()) ? \
458 PAGE_EXECUTE_READWRITE|PAGE_GUARD : PAGE_NOACCESS)
461 alloc_stack_hard_way (child_info_fork *ci, volatile char *b)
463 void *new_stack_pointer;
464 MEMORY_BASIC_INFORMATION m;
467 LPBYTE curbot = (LPBYTE) sm.BaseAddress + sm.RegionSize;
470 if (ci->stacktop > (LPBYTE) sm.AllocationBase && ci->stacktop < curbot)
473 newlen = (LPBYTE) ci->stackbottom - (LPBYTE) curbot;
478 newbase = ci->stacktop;
479 newlen = (DWORD) ci->stackbottom - (DWORD) ci->stacktop;
482 if (!VirtualAlloc (newbase, newlen, MEM_RESERVE, PAGE_NOACCESS))
483 api_fatal ("fork: can't reserve memory for stack %p - %p, %E",
484 ci->stacktop, ci->stackbottom);
486 new_stack_pointer = (void *) ((LPBYTE) ci->stackbottom - ci->stacksize);
488 if (!VirtualAlloc (new_stack_pointer, ci->stacksize, MEM_COMMIT,
489 PAGE_EXECUTE_READWRITE))
490 api_fatal ("fork: can't commit memory for stack %p(%d), %E",
491 new_stack_pointer, ci->stacksize);
492 if (!VirtualQuery ((LPCVOID) new_stack_pointer, &m, sizeof m))
493 api_fatal ("fork: couldn't get new stack info, %E");
496 m.BaseAddress = (LPVOID) ((DWORD) m.BaseAddress - 1);
497 if (!VirtualAlloc ((LPVOID) m.BaseAddress, 1, MEM_COMMIT,
499 api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
502 if (!VirtualQuery ((LPCVOID) m.BaseAddress, &m, sizeof m))
503 api_fatal ("fork: couldn't get new stack info, %E");
504 ci->stacktop = m.BaseAddress;
508 void *getstack (void *) __attribute__ ((noinline));
510 getstack (volatile char * volatile p)
517 /* extend the stack prior to fork longjmp */
520 alloc_stack (child_info_fork *ci)
522 volatile char * volatile esp;
523 __asm__ volatile ("movl %%esp,%0": "=r" (esp));
524 if (_tlsbase != ci->stackbottom)
525 alloc_stack_hard_way (ci, esp);
528 char *stacktop = (char *) ci->stacktop - 4096;
529 while (_tlstop >= stacktop)
530 esp = getstack (esp);
539 debug_printf ("break here");
546 char buf[CYG_MAX_PATH];
547 if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
553 if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
555 DWORD ms = atoi (buf);
557 len = GetModuleFileName (NULL, buf, CYG_MAX_PATH);
558 console_printf ("Sleeping %d, pid %u %s\n", ms, GetCurrentProcessId (), buf);
560 if (!strace.active () && !dynamically_loaded)
563 if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
565 char buf1[CYG_MAX_PATH];
566 len = GetModuleFileName (NULL, buf1, CYG_MAX_PATH);
569 char *p = strpbrk (buf, ":=");
571 p = (char *) "gdb.exe -nw";
574 if (strstr (buf1, buf))
576 error_start_init (p);
578 console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n");
588 get_cygwin_startup_info ()
591 char zeros[sizeof (child_proc_info->zero)] = {0};
593 GetStartupInfo (&si);
594 child_info *res = (child_info *) si.lpReserved2;
595 if (si.cbReserved2 < EXEC_MAGIC_SIZE || !res
596 || memcmp (res->zero, zeros, sizeof (res->zero)) != 0)
600 if ((res->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
601 multiple_cygwin_problem ("proc intro", res->intro, 0);
602 else if (res->intro == PROC_MAGIC_GENERIC
603 && res->magic != CHILD_INFO_MAGIC)
604 multiple_cygwin_problem ("proc magic", res->magic,
606 else if (res->cygheap != (void *) &_cygheap_start)
607 multiple_cygwin_problem ("cygheap base", (DWORD) res->cygheap,
608 (DWORD) &_cygheap_start);
610 unsigned should_be_cb = 0;
615 should_be_cb = sizeof (child_info_fork);
620 should_be_cb = sizeof (child_info_spawn);
621 if (should_be_cb != res->cb)
622 multiple_cygwin_problem ("proc size", res->cb, should_be_cb);
623 else if (sizeof (fhandler_union) != res->fhandler_union_cb)
624 multiple_cygwin_problem ("fhandler size", res->fhandler_union_cb, sizeof (fhandler_union));
628 for (unsigned i = 0; !being_debugged () && i < 10000; i++)
629 low_priority_sleep (0);
634 system_printf ("unknown exec type %d", res->type);
635 /* intentionally fall through */
646 child_info_fork::fork_retry (HANDLE h)
649 if (!GetExitCodeProcess (h, &exit_code))
650 return STILL_ACTIVE; /* should never happen */
653 case STATUS_CONTROL_C_EXIT:
666 child_info_fork::handle_failure (DWORD err)
669 ExitProcess (EXITCODE_RETRY);
673 #define dll_data_start &_data_start__
674 #define dll_data_end &_data_end__
675 #define dll_bss_start &_bss_start__
676 #define dll_bss_end &_bss_end__
679 child_info_fork::handle_fork ()
681 cygheap_fixup_in_child (false);
684 child_copy (parent, false,
685 "dll data", dll_data_start, dll_data_end,
686 "dll bss", dll_bss_start, dll_bss_end,
687 "user heap", cygheap->user_heap.base, cygheap->user_heap.ptr,
689 /* step 2 now that the dll has its heap filled in, we can fill in the
690 user's data and bss since user_data is now filled out. */
691 child_copy (parent, false,
692 "data", user_data->data_start, user_data->data_end,
693 "bss", user_data->bss_start, user_data->bss_end,
696 if (fixup_mmaps_after_fork (parent))
697 api_fatal ("recreate_mmaps_after_fork_failed");
703 init_global_security ();
706 /* Initialize signal processing here, early, in the hopes that the creation
707 of a thread early in the process will cause more predictability in memory
708 layout for the main thread. */
711 lock_process::init ();
712 init_console_handler (TRUE);
713 _impure_ptr = _GLOBAL_REENT;
714 _impure_ptr->_stdin = &_impure_ptr->__sf[0];
715 _impure_ptr->_stdout = &_impure_ptr->__sf[1];
716 _impure_ptr->_stderr = &_impure_ptr->__sf[2];
717 _impure_ptr->_current_locale = "C";
718 user_data->impure_ptr = _impure_ptr;
719 user_data->impure_ptr_ptr = &_impure_ptr;
722 if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
723 GetCurrentProcess (), &hMainProc, 0, FALSE,
724 DUPLICATE_SAME_ACCESS))
725 hMainProc = GetCurrentProcess ();
727 DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
728 &hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
729 if (wincap.has_security ())
730 OpenProcessToken (hMainProc, MAXIMUM_ALLOWED, &hProcToken);
732 SetErrorMode (SEM_FAILCRITICALERRORS);
734 do_global_ctors (&__CTOR_LIST__, 1);
737 child_proc_info = get_cygwin_startup_info ();
738 if (!child_proc_info)
742 cygwin_user_h = child_proc_info->user_h;
743 switch (child_proc_info->type)
746 fork_info->handle_fork ();
751 cygheap_fixup_in_child (true);
753 if (!spawn_info->moreinfo->myself_pinfo ||
754 !DuplicateHandle (hMainProc, spawn_info->moreinfo->myself_pinfo,
755 hMainProc, &h, 0, FALSE,
756 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
759 if (child_proc_info->type != _PROC_FORK)
760 child_proc_info->ready (true);
761 __argc = spawn_info->moreinfo->argc;
762 __argv = spawn_info->moreinfo->argv;
763 envp = spawn_info->moreinfo->envp;
764 envc = spawn_info->moreinfo->envc;
765 if (!dynamically_loaded)
766 cygheap->fdtab.fixup_after_exec ();
767 signal_fixup_after_exec ();
768 if (spawn_info->moreinfo->old_title)
770 old_title = strcpy (title_buf, spawn_info->moreinfo->old_title);
771 cfree (spawn_info->moreinfo->old_title);
777 user_data->resourcelocks->Init ();
778 user_data->threadinterface->Init ();
782 pinfo_init (envp, envc);
783 uinfo_init (); /* initialize user info */
787 /* Initialize events */
790 cygheap->cwd.init ();
792 /* Late duplicate simplifies tweaking the process token in uinfo.cc. */
793 if (wincap.has_security ())
794 DuplicateTokenEx (hProcToken, MAXIMUM_ALLOWED, NULL,
795 SecurityImpersonation, TokenImpersonation,
797 debug_printf ("finished dll_crt0_0 initialization");
800 /* Take over from libc's crt0.o and start the application. Note the
801 various special cases when Cygwin DLL is being runtime loaded (as
802 opposed to being link-time loaded by Cygwin apps) from a non
803 cygwin app via LoadLibrary. */
807 check_sanity_and_sync (user_data);
811 const int n = 2 * 1024 * 1024;
813 small_printf ("cmalloc returns %p\n", cmalloc (HEAP_STR, n));
816 ProtectHandle (hMainProc);
817 ProtectHandle (hMainThread);
819 /* Initialize pthread mainthread when not forked and it is safe to call new,
820 otherwise it is reinitalized in fixup_after_fork */
822 pthread::init_mainthread ();
825 strace.microseconds ();
828 create_signal_arrived ();
830 /* Initialize debug muto, if DLL is built with --enable-debugging.
831 Need to do this before any helper threads start. */
835 cygheap->fdtab.vfork_child_fixup ();
836 main_vfork = vfork_storage.create ();
839 cygbench ("pre-forkee");
842 /* If we've played with the stack, stacksize != 0. That means that
843 fork() was invoked from other than the main thread. Make sure that
844 frame pointer is referencing the new stack so that the OS knows what
845 to do when it needs to increase the size of the stack.
847 NOTE: Don't do anything that involves the stack until you've completed
849 if (fork_info->stacksize)
851 _tlsbase = (char *) fork_info->stackbottom;
852 _tlstop = (char *) fork_info->stacktop;
854 longjmp (fork_info->jmp, true);
859 extern void fork_init ();
864 /* Can be set only after environment has been initialized. */
865 if (wincap.has_security ())
866 set_cygwin_privileges (hProcImpToken);
868 if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
869 old_title = title_buf;
871 /* Allocate cygheap->fdtab */
874 /* Connect to tty. */
879 char *line = GetCommandLineA ();
880 line = strcpy ((char *) alloca (strlen (line) + 1), line);
882 if (current_codepage == oem_cp)
883 CharToOemA (line, line);
885 /* Scan the command line and build argv. Expand wildcards if not
886 called from another cygwin process. */
887 build_argv (line, __argv, __argc,
888 NOTSTATE (myself, PID_CYGPARENT) && allow_glob);
890 /* Convert argv[0] to posix rules if it's currently blatantly
892 if ((strchr (__argv[0], ':')) || (strchr (__argv[0], '\\')))
894 char *new_argv0 = (char *) malloc (CYG_MAX_PATH);
895 cygwin_conv_to_posix_path (__argv[0], new_argv0);
896 __argv[0] = (char *) realloc (new_argv0, strlen (new_argv0) + 1);
900 __argc_safe = __argc;
901 if (user_data->premain[0])
902 for (unsigned int i = 0; i < PREMAIN_LEN / 2; i++)
903 user_data->premain[i] (__argc, __argv, user_data);
905 /* Set up standard fds in file descriptor table. */
906 cygheap->fdtab.stdio_init ();
908 /* Set up __progname for getopt error call. */
909 if (__argv[0] && (__progname = strrchr (__argv[0], '/')))
912 __progname = __argv[0];
915 char *cp = strchr (__progname, '\0') - 4;
916 if (cp > __progname && strcasematch (cp, ".exe"))
920 /* Set new console title if appropriate. */
922 if (display_title && !dynamically_loaded)
924 char *cp = __progname;
925 if (strip_title_path)
926 for (char *ptr = cp; *ptr && *ptr != ' '; ptr++)
929 set_console_title (cp);
932 cygwin_finished_initializing = true;
933 /* Call init of loaded dlls. */
936 /* Execute any specified "premain" functions */
937 if (user_data->premain[PREMAIN_LEN / 2])
938 for (unsigned int i = PREMAIN_LEN / 2; i < PREMAIN_LEN; i++)
939 user_data->premain[i] (__argc, __argv, user_data);
941 debug_printf ("user_data->main %p", user_data->main);
943 if (dynamically_loaded)
949 /* Disable case-insensitive globbing */
950 ignore_case_with_glob = false;
955 cygbench (__progname);
957 /* Flush signals and ensure that signal thread is up and running. Can't
958 do this for noncygwin case since the signal thread is blocked due to
959 LoadLibrary serialization. */
962 cygwin_exit (user_data->main (__argc, __argv, *user_data->envptr));
966 initialize_main_tls (char *padding)
970 _main_tls = &_my_tls;
971 _main_tls->init_thread (padding, NULL);
973 return &_main_tls->local_clib;
976 /* Wrap the real one, otherwise gdb gets confused about
977 two symbols with the same name, but different addresses.
979 UPTR is a pointer to global data that lives on the libc side of the
980 line [if one distinguishes the application from the dll]. */
982 extern "C" void __stdcall
985 extern DWORD threadfunc_ix;
986 wait_for_sigthread ();
988 system_printf ("internal error: couldn't determine location of thread function on stack. Expect signal problems.");
990 main_environ = user_data->envptr;
993 char padding[CYGTLS_PADSIZE];
996 alloc_stack (fork_info);
998 __sinit (_impure_ptr);
1000 initialize_main_tls (padding);
1001 dll_crt0_1 (padding);
1005 dll_crt0 (per_process *uptr)
1007 /* Set the local copy of the pointer into the user space. */
1008 if (!in_forkee && uptr && uptr != user_data)
1010 memcpy (user_data, uptr, per_process_overwrite);
1011 *(user_data->impure_ptr_ptr) = _GLOBAL_REENT;
1016 /* This must be called by anyone who uses LoadLibrary to load cygwin1.dll.
1017 You must have CYGTLS_PADSIZE bytes reserved at the bottom of the stack
1018 calling this function, and that storage must not be overwritten until you
1019 unload cygwin1.dll, as it is used for _my_tls. It is best to load
1020 cygwin1.dll before spawning any additional threads in your process.
1022 See winsup/testsuite/cygload for an example of how to use cygwin1.dll
1023 from MSVC and non-cygwin MinGW applications. */
1030 user_data->magic_biscuit = sizeof (per_process);
1032 user_data->envptr = &envp;
1033 user_data->fmode_ptr = &_fmode;
1041 do_global_ctors (user_data->ctors, false);
1042 atexit (do_global_dtors);
1045 exit_states NO_COPY exit_state;
1048 do_exit (int status)
1050 syscall_printf ("do_exit (%d), exit_state %d", status, exit_state);
1053 vfork_save *vf = vfork_storage.val ();
1054 if (vf != NULL && vf->pid < 0)
1056 exit_state = ES_NOT_EXITING;
1057 vf->restore_exit (status);
1061 lock_process until_exit (true);
1063 if (exit_state < ES_GLOBAL_DTORS)
1065 exit_state = ES_GLOBAL_DTORS;
1066 dll_global_dtors ();
1069 if (exit_state < ES_EVENTS_TERMINATE)
1071 exit_state = ES_EVENTS_TERMINATE;
1072 events_terminate ();
1075 UINT n = (UINT) status;
1076 if (exit_state < ES_THREADTERM)
1078 exit_state = ES_THREADTERM;
1079 cygthread::terminate ();
1082 if (exit_state < ES_SIGNAL)
1084 exit_state = ES_SIGNAL;
1085 signal (SIGCHLD, SIG_IGN);
1086 signal (SIGHUP, SIG_IGN);
1087 signal (SIGINT, SIG_IGN);
1088 signal (SIGQUIT, SIG_IGN);
1091 if (exit_state < ES_CLOSEALL)
1093 exit_state = ES_CLOSEALL;
1097 myself->stopsig = 0;
1099 if (exit_state < ES_HUP_PGRP)
1101 exit_state = ES_HUP_PGRP;
1102 /* Kill orphaned children on group leader exit */
1103 if (myself->has_pgid_children && myself->pid == myself->pgid)
1106 si.si_signo = -SIGHUP;
1107 si.si_code = SI_KERNEL;
1108 sigproc_printf ("%d == pgrp %d, send SIG{HUP,CONT} to stopped children",
1109 myself->pid, myself->pgid);
1110 kill_pgrp (myself->pgid, si);
1114 if (exit_state < ES_HUP_SID)
1116 exit_state = ES_HUP_SID;
1117 /* Kill the foreground process group on session leader exit */
1118 if (getpgrp () > 0 && myself->pid == myself->sid && real_tty_attached (myself))
1120 tty *tp = cygwin_shared->tty[myself->ctty];
1121 sigproc_printf ("%d == sid %d, send SIGHUP to children",
1122 myself->pid, myself->sid);
1124 /* CGF FIXME: This can't be right. */
1125 if (tp->getsid () == myself->sid)
1126 tp->kill_pgrp (SIGHUP);
1131 if (exit_state < ES_TITLE)
1133 exit_state = ES_TITLE;
1134 /* restore console title */
1135 if (old_title && display_title)
1136 set_console_title (old_title);
1139 if (exit_state < ES_TTY_TERMINATE)
1141 exit_state = ES_TTY_TERMINATE;
1148 static NO_COPY muto atexit_lock;
1151 cygwin_atexit (void (*function)(void))
1154 atexit_lock.init ("atexit_lock");
1155 atexit_lock.acquire ();
1156 res = atexit (function);
1157 atexit_lock.release ();
1164 dll_global_dtors ();
1166 atexit_lock.acquire ();
1173 do_exit (((DWORD) n & 0xff) << 8);
1177 __api_fatal (const char *fmt, ...)
1183 int n = __small_sprintf (buf, "%P: *** fatal error - ", cygwin_pid (GetCurrentProcessId ()));
1184 __small_vsprintf (buf + n, fmt, ap);
1186 strace.prntf (_STRACE_SYSTEM, NULL, "%s", buf);
1191 myself.exit (__api_fatal_exit_val);
1195 multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned version)
1197 if (_cygwin_testing && (strstr (what, "proc") || strstr (what, "cygheap")))
1199 child_proc_info->type = _PROC_WHOOPS;
1204 if (GetEnvironmentVariable ("CYGWIN_MISMATCH_OK", buf, sizeof (buf)))
1207 if (CYGWIN_VERSION_MAGIC_VERSION (magic_version) == version)
1208 system_printf ("%s magic number mismatch detected - %p/%p", what, magic_version, version);
1210 api_fatal ("%s mismatch detected - %p/%p.\n\
1211 This problem is probably due to using incompatible versions of the cygwin DLL.\n\
1212 Search for cygwin1.dll using the Windows Start->Find/Search facility\n\
1213 and delete all but the most recent version. The most recent version *should*\n\
1214 reside in x:\\cygwin\\bin, where 'x' is the drive on which you have\n\
1215 installed the cygwin distribution. Rebooting is also suggested if you\n\
1216 are unable to find another cygwin DLL.",
1217 what, magic_version, version);
1222 cygbench (const char *s)
1225 if (GetEnvironmentVariable ("CYGWIN_BENCH", buf, sizeof (buf)))
1226 small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());