OSDN Git Service

* globals.cc (enum exit_states::ES_GLOBAL_DTORS): Delete.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / dcrt0.cc
index 1c5464f..9cff06f 100644 (file)
@@ -1,7 +1,8 @@
 /* dcrt0.cc -- essentially the main() for the Cygwin dll
 
-   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2006, 2007 Red Hat, Inc.
+   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
+   2007, 2008, 2009
+   Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -10,23 +11,18 @@ Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
 details. */
 
 #include "winsup.h"
+#include "miscfuncs.h"
 #include <unistd.h>
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include "glob.h"
-#include "exceptions.h"
 #include <ctype.h>
-#include <limits.h>
-#include <winnls.h>
-#include <wingdi.h>
-#include <winuser.h>
+#include <locale.h>
+#include "environ.h"
 #include "sigproc.h"
 #include "pinfo.h"
 #include "cygerrno.h"
 #define NEED_VFORK
 #include "perprocess.h"
-#include "security.h"
 #include "path.h"
 #include "fhandler.h"
 #include "dtable.h"
@@ -36,98 +32,23 @@ details. */
 #include "shared_info.h"
 #include "cygwin_version.h"
 #include "dll_init.h"
-#include "sync.h"
 #include "heap.h"
-#include "environ.h"
+#include "tls_pbuf.h"
 
 #define MAX_AT_FILE_LEVEL 10
 
 #define PREMAIN_LEN (sizeof (user_data->premain) / sizeof (user_data->premain[0]))
 
-extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
-
-HANDLE NO_COPY hMainProc = (HANDLE) -1;
-HANDLE NO_COPY hMainThread;
-HANDLE NO_COPY hProcToken;
-HANDLE NO_COPY hProcImpToken;
-
-muto NO_COPY lock_process::locker;
-
-bool display_title;
-bool strip_title_path;
-bool allow_glob = true;
-bool NO_COPY in_forkee;
-
-int __argc_safe;
-int _declspec(dllexport) __argc;
-char _declspec(dllexport) **__argv;
-#ifdef NEWVFORK
-vfork_save NO_COPY *main_vfork;
-#endif
-
-static int NO_COPY envc;
-char NO_COPY **envp;
 
+extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
 extern "C" void __sinit (_reent *);
 
-_cygtls NO_COPY *_main_tls;
-
-bool NO_COPY cygwin_finished_initializing;
-
-/* Used in SIGTOMASK for generating a bit for insertion into a sigset_t.
-   This is subtracted from the signal number prior to shifting the bit.
-   In older versions of cygwin, the signal was used as-is to shift the
-   bit for masking.  So, we'll temporarily detect this and set it to zero
-   for programs that are linked using older cygwins.  This is just a stopgap
-   measure to allow an orderly transfer to the new, correct sigmask method. */
-unsigned NO_COPY int signal_shift_subtract = 1;
-
-MTinterface _mtinterf;
-
-bool NO_COPY _cygwin_testing;
+static int NO_COPY envc;
+static char NO_COPY **envp;
 
-char NO_COPY almost_null[1];
+static char title_buf[TITLESIZE + 1];
 
-extern "C"
-{
-  /* This is an exported copy of environ which can be used by DLLs
-     which use cygwin.dll.  */
-  char **__cygwin_environ;
-  char ***main_environ = &__cygwin_environ;
-  /* __progname used in getopt error message */
-  char *__progname;
-  struct per_process __cygwin_user_data =
-  {/* initial_sp */ 0, /* magic_biscuit */ 0,
-   /* dll_major */ CYGWIN_VERSION_DLL_MAJOR,
-   /* dll_major */ CYGWIN_VERSION_DLL_MINOR,
-   /* impure_ptr_ptr */ NULL, /* envptr */ NULL,
-   /* malloc */ malloc, /* free */ free,
-   /* realloc */ realloc,
-   /* fmode_ptr */ NULL, /* main */ NULL, /* ctors */ NULL,
-   /* dtors */ NULL, /* data_start */ NULL, /* data_end */ NULL,
-   /* bss_start */ NULL, /* bss_end */ NULL,
-   /* calloc */ calloc,
-   /* premain */ {NULL, NULL, NULL, NULL},
-   /* run_ctors_p */ 0,
-   /* unused */ {0, 0, 0, 0, 0, 0, 0},
-   /* UNUSED forkee */ 0,
-   /* hmodule */ NULL,
-   /* api_major */ CYGWIN_VERSION_API_MAJOR,
-   /* api_minor */ CYGWIN_VERSION_API_MINOR,
-   /* unused2 */ {0, 0, 0, 0, 0, 0},
-   /* threadinterface */ &_mtinterf,
-   /* impure_ptr */ _GLOBAL_REENT,
-  };
-  bool ignore_case_with_glob;
-  int __declspec (dllexport) _check_for_executable = true;
-#ifdef DEBUGGING
-  int pinger;
-#endif
-  int NO_COPY __api_fatal_exit_val = 1;
-};
-
-char *old_title;
-char title_buf[TITLESIZE + 1];
+bool NO_COPY jit_debug;
 
 static void
 do_global_dtors ()
@@ -169,14 +90,17 @@ insert_file (char *name, char *&cmd)
 {
   HANDLE f;
   DWORD size;
-
-  f = CreateFile (name + 1,
-                 GENERIC_READ,          /* open for reading    */
-                 FILE_SHARE_READ,       /* share for reading   */
-                 &sec_none_nih,         /* no security         */
-                 OPEN_EXISTING,         /* existing file only  */
-                 FILE_ATTRIBUTE_NORMAL, /* normal file         */
-                 NULL);                 /* no attr. template   */
+  tmp_pathbuf tp;
+
+  PWCHAR wname = tp.w_get ();
+  sys_mbstowcs (wname, NT_MAX_PATH, name + 1);
+  f = CreateFileW (wname,
+                  GENERIC_READ,         /* open for reading    */
+                  FILE_SHARE_READ,      /* share for reading   */
+                  &sec_none_nih,        /* default security    */
+                  OPEN_EXISTING,        /* existing file only  */
+                  FILE_ATTRIBUTE_NORMAL,/* normal file         */
+                  NULL);                /* no attr. template   */
 
   if (f == INVALID_HANDLE_VALUE)
     {
@@ -307,7 +231,15 @@ globify (char *word, char **&argv, int &argc, int &argvlen)
            else if (s[1] == quote || s[1] == '\\')
              s++;
            *p++ = '\\';
-           *p++ = *s;
+           size_t cnt = isascii (*s) ? 1 : mbtowc (NULL, s, MB_CUR_MAX);
+           if (cnt <= 1 || cnt == (size_t)-1)
+             *p++ = *s;
+           else
+             {
+               --s;
+               while (cnt-- > 0)
+                 *p++ = *++s;
+             }
          }
        if (*s == quote)
          p--;
@@ -443,10 +375,6 @@ check_sanity_and_sync (per_process *p)
   if (p->api_major > cygwin_version.api_major)
     api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %d > %d",
               p->api_major, cygwin_version.api_major);
-
-  if (CYGWIN_VERSION_DLL_MAKE_COMBINED (p->dll_major, p->dll_minor) <=
-      CYGWIN_VERSION_DLL_BAD_SIGNAL_MASK)
-    signal_shift_subtract = 0;
 }
 
 child_info NO_COPY *child_proc_info = NULL;
@@ -544,14 +472,14 @@ break_here ()
 static void
 initial_env ()
 {
-  char buf[PATH_MAX];
-  if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
+  if (GetEnvironmentVariableA ("CYGWIN_TESTING", NULL, 0))
     _cygwin_testing = 1;
 
 #ifdef DEBUGGING
+  char buf[NT_MAX_PATH];
   DWORD len;
 
-  if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
+  if (GetEnvironmentVariableA ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
     {
       DWORD ms = atoi (buf);
       console_printf ("Sleeping %d, pid %u %P\n", ms, GetCurrentProcessId ());
@@ -559,10 +487,10 @@ initial_env ()
       if (!strace.active () && !dynamically_loaded)
        strace.hello ();
     }
-  if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
+  if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
     {
-      char buf1[PATH_MAX];
-      len = GetModuleFileName (NULL, buf1, PATH_MAX);
+      char buf1[NT_MAX_PATH];
+      len = GetModuleFileName (NULL, buf1, NT_MAX_PATH);
       strlwr (buf1);
       strlwr (buf);
       char *p = strpbrk (buf, ":=");
@@ -573,6 +501,7 @@ initial_env ()
       if (strstr (buf1, buf))
        {
          error_start_init (p);
+         jit_debug = true;
          try_to_debug ();
          console_printf ("*** Sending Break.  gdb may issue spurious SIGTRAP message.\n");
          break_here ();
@@ -645,8 +574,8 @@ void
 child_info_fork::handle_fork ()
 {
   cygheap_fixup_in_child (false);
-  memory_init ();
-  set_myself (NULL);
+  memory_init (false);
+  myself.thisproc (NULL);
   myself->uid = cygheap->user.real_uid;
   myself->gid = cygheap->user.real_gid;
 
@@ -669,14 +598,15 @@ child_info_fork::handle_fork ()
 void
 child_info_spawn::handle_spawn ()
 {
+  extern void fixup_lockf_after_exec ();
   HANDLE h;
   cygheap_fixup_in_child (true);
-  memory_init ();
+  memory_init (false);
   if (!moreinfo->myself_pinfo ||
       !DuplicateHandle (hMainProc, moreinfo->myself_pinfo, hMainProc, &h, 0,
                        FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
     h = NULL;
-  set_myself (h);
+  myself.thisproc (h);
   __argc = moreinfo->argc;
   __argv = moreinfo->argv;
   envp = moreinfo->envp;
@@ -687,6 +617,7 @@ child_info_spawn::handle_spawn ()
     cygheap->fdtab.move_fd (__stdin, 0);
   if (__stdout >= 0)
     cygheap->fdtab.move_fd (__stdout, 1);
+  cygheap->user.groups.clear_supp ();
 
   ready (true);
 
@@ -701,20 +632,52 @@ child_info_spawn::handle_spawn ()
       old_title = strcpy (title_buf, moreinfo->old_title);
       cfree (moreinfo->old_title);
     }
+  fixup_lockf_after_exec ();
 }
 
+#if 0
+/* Setting the TS-aware flag in the application's PE header is sufficient.
+   Just keep this in as a reminder. */
+
+static DEP_SYSTEM_POLICY_TYPE dep_system_policy = (DEP_SYSTEM_POLICY_TYPE) -1;
+
+static void
+disable_dep ()
+{
+  DWORD ppolicy;
+  BOOL perm;
+
+  if (dep_system_policy < 0)
+    {
+      dep_system_policy = GetSystemDEPPolicy ();
+      debug_printf ("DEP System Policy: %d", (int) dep_system_policy);
+    }
+  if (dep_system_policy < OptIn)
+    return;
+  if (!GetProcessDEPPolicy (hMainProc, &ppolicy, &perm))
+    {
+      debug_printf ("GetProcessDEPPolicy: %E");
+      return;
+    }
+  debug_printf ("DEP Process Policy: %d (permanent = %d)", ppolicy, perm);
+  if (ppolicy > 0 && !perm && !SetProcessDEPPolicy (0))
+    debug_printf ("SetProcessDEPPolicy: %E");
+}
+#endif
+
 void __stdcall
 dll_crt0_0 ()
 {
   init_global_security ();
   initial_env ();
 
-  SetErrorMode (SEM_FAILCRITICALERRORS);
+  SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
 
   /* Initialize signal processing here, early, in the hopes that the creation
      of a thread early in the process will cause more predictability in memory
      layout for the main thread. */
-  sigproc_init ();
+  if (!dynamically_loaded)
+    sigproc_init ();
 
   lock_process::init ();
   _impure_ptr = _GLOBAL_REENT;
@@ -742,7 +705,7 @@ dll_crt0_0 ()
 
   child_proc_info = get_cygwin_startup_info ();
   if (!child_proc_info)
-    memory_init ();
+    memory_init (true);
   else
     {
       cygwin_user_h = child_proc_info->user_h;
@@ -766,7 +729,28 @@ dll_crt0_0 ()
   events_init ();
   tty_list::init_session ();
 
-  cygheap->cwd.init ();
+#if 0
+  /* Setting the TS-aware flag in the application's PE header is sufficient.
+     Just keep this in as a reminder. */
+
+  /* The disable_dep function disables DEP for all Cygwin processes if
+     the process runs on a Windows Server 2008 with Terminal Services
+     installed.  This combination (TS+DEP) breaks *some* Cygwin
+     applications.  The Terminal Service specific DLL tsappcmp.dll
+     changes the page protection of some pages in the application's text
+     segment from PAGE_EXECUTE_WRITECOPY to PAGE_WRITECOPY for no
+     apparent reason.  This occurs before any Cygwin or applicaton code
+     had a chance to run.  MS has no explanation for this so far, but is
+     rather busy trying to avoid giving support for this problem (as of
+     2008-11-11).
+
+     Unfortunately disabling DEP seems to have a not negligible
+     performance hit.  In the long run, either MS has to fix their
+     problem, or we have to find a better workaround, if any exists.
+     Idle idea: Adding EXECUTE protection to all text segment pages? */
+  if (wincap.ts_has_dep_problem ())
+    disable_dep ();
+#endif
 
   debug_printf ("finished dll_crt0_0 initialization");
 }
@@ -778,8 +762,17 @@ dll_crt0_0 ()
 void
 dll_crt0_1 (void *)
 {
+  if (dynamically_loaded)
+    sigproc_init ();
   check_sanity_and_sync (user_data);
+
+  /* Initialize malloc and then call user_shared_initialize since it relies
+     on a functioning malloc and it's possible that the user's program may
+     have overridden malloc.  We only know about that at this stage,
+     unfortunately. */
   malloc_init ();
+  user_shared_initialize ();
+
 #ifdef CGF
   int i = 0;
   const int n = 2 * 1024 * 1024;
@@ -790,6 +783,8 @@ dll_crt0_1 (void *)
   ProtectHandle (hMainProc);
   ProtectHandle (hMainThread);
 
+  cygheap->cwd.init ();
+
   /* Initialize pthread mainthread when not forked and it is safe to call new,
      otherwise it is reinitalized in fixup_after_fork */
   if (!in_forkee)
@@ -857,7 +852,7 @@ dll_crt0_1 (void *)
   if (!__argc)
     {
       PWCHAR wline = GetCommandLineW ();
-      size_t size = sys_wcstombs (NULL, size, wline);
+      size_t size = sys_wcstombs (NULL, 0, wline);
       char *line = (char *) alloca (size);
       sys_wcstombs (line, size, wline);
 
@@ -870,8 +865,9 @@ dll_crt0_1 (void *)
         win32 style. */
       if ((strchr (__argv[0], ':')) || (strchr (__argv[0], '\\')))
        {
-         char *new_argv0 = (char *) malloc (PATH_MAX);
-         cygwin_conv_to_posix_path (__argv[0], new_argv0);
+         char *new_argv0 = (char *) malloc (NT_MAX_PATH);
+         cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, __argv[0],
+                           new_argv0, NT_MAX_PATH);
          __argv[0] = (char *) realloc (new_argv0, strlen (new_argv0) + 1);
        }
     }
@@ -937,8 +933,14 @@ dll_crt0_1 (void *)
      do this for noncygwin case since the signal thread is blocked due to
      LoadLibrary serialization. */
   ld_preload ();
+  /* Reset current application locale to "C" per POSIX */
+  _setlocale_r (_REENT, LC_CTYPE, "C");
   if (user_data->main)
     cygwin_exit (user_data->main (__argc, __argv, *user_data->envptr));
+  __asm__ ("                           \n\
+       .global __cygwin_exit_return    \n\
+__cygwin_exit_return:                  \n\
+");
 }
 
 extern "C" void __stdcall
@@ -991,12 +993,19 @@ cygwin_dll_init ()
 extern "C" void
 __main (void)
 {
+  /* Ordering is critical here.  DLL ctors have already been
+     run as they were being loaded, so we should stack the 
+     queued call to DLL dtors now.  */
+  atexit (dll_global_dtors);
   do_global_ctors (user_data->ctors, false);
+  /* Now we have run global ctors, register their dtors.  */
   atexit (do_global_dtors);
+  /* At exit, global dtors will run first, so the app can still
+     use shared library functions while terminating; then the
+     DLLs will be destroyed; finally newlib will shut down stdio
+     and terminate itself.  */
 }
 
-exit_states NO_COPY exit_state;
-
 void __stdcall
 do_exit (int status)
 {
@@ -1013,12 +1022,6 @@ do_exit (int status)
 
   lock_process until_exit (true);
 
-  if (exit_state < ES_GLOBAL_DTORS)
-    {
-      exit_state = ES_GLOBAL_DTORS;
-      dll_global_dtors ();
-    }
-
   if (exit_state < ES_EVENTS_TERMINATE)
     {
       exit_state = ES_EVENTS_TERMINATE;
@@ -1114,7 +1117,6 @@ cygwin_atexit (void (*function)(void))
 extern "C" void
 cygwin_exit (int n)
 {
-  dll_global_dtors ();
   if (atexit_lock)
     atexit_lock.acquire ();
   exit (n);
@@ -1153,8 +1155,7 @@ multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned vers
       return;
     }
 
-  char buf[1024];
-  if (GetEnvironmentVariable ("CYGWIN_MISMATCH_OK", buf, sizeof (buf)))
+  if (GetEnvironmentVariableA ("CYGWIN_MISMATCH_OK", NULL, 0))
     return;
 
   if (CYGWIN_VERSION_MAGIC_VERSION (magic_version) == version)
@@ -1174,8 +1175,7 @@ are unable to find another cygwin DLL.",
 void __stdcall
 cygbench (const char *s)
 {
-  char buf[1024];
-  if (GetEnvironmentVariable ("CYGWIN_BENCH", buf, sizeof (buf)))
+  if (GetEnvironmentVariableA ("CYGWIN_BENCH", NULL, 0))
     small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
 }
 #endif