From e2e078278cc951344fc5c0a976715bd1fe11f2d8 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Fri, 14 Sep 2001 00:49:00 +0000 Subject: [PATCH] * cygheap.cc (dup_now): New function. (cygheap_setup_for_child): Accept new argument controlling whether to delay copying of cygheap to shared memory region. (cygheap_setup_for_child_cleanup): Accept new arguments controlling whether to copy cygheap at this point. * cygheap.h: Reflect above changes. * fork.cc (fork_parent): Break copying of cygheap into two parts when fork_fixup is required so that the child can see the parent's changes. (vfork): Do stack cleanup prior to forcing a fork error. * spawn.cc (spawn_guts): Ditto. --- winsup/cygwin/ChangeLog | 13 +++++++++++++ winsup/cygwin/cygheap.cc | 38 ++++++++++++++++++++++++++++---------- winsup/cygwin/cygheap.h | 4 ++-- winsup/cygwin/fork.cc | 21 +++++++++++++-------- winsup/cygwin/spawn.cc | 12 ++++++++---- 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 8217be643..acb342047 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,16 @@ +Thu Sep 13 20:46:05 2001 Christopher Faylor + + * cygheap.cc (dup_now): New function. + (cygheap_setup_for_child): Accept new argument controlling whether to + delay copying of cygheap to shared memory region. + (cygheap_setup_for_child_cleanup): Accept new arguments controlling + whether to copy cygheap at this point. + * cygheap.h: Reflect above changes. + * fork.cc (fork_parent): Break copying of cygheap into two parts when + fork_fixup is required so that the child can see the parent's changes. + (vfork): Do stack cleanup prior to forcing a fork error. + * spawn.cc (spawn_guts): Ditto. + Thu Sep 13 17:14:59 2001 Christopher Faylor * cygheap.cc (ccalloc): Pass correct length to creturn so that diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc index 4866a7e8d..c5f6af776 100644 --- a/winsup/cygwin/cygheap.cc +++ b/winsup/cygwin/cygheap.cc @@ -64,8 +64,17 @@ init_cheap () cygheap_max = cygheap + 1; } -void __stdcall -cygheap_setup_for_child (child_info *ci) +static void dup_now (void *, child_info *, unsigned) __attribute__ ((regparm(3))); +static void +dup_now (void *newcygheap, child_info *ci, unsigned n) +{ + if (!VirtualAlloc (newcygheap, n, MEM_COMMIT, PAGE_READWRITE)) + api_fatal ("couldn't allocate new cygwin heap for child, %E"); + memcpy (newcygheap, cygheap, n); +} + +void *__stdcall +cygheap_setup_for_child (child_info *ci, bool dup_later) { void *newcygheap; cygheap_protect->acquire (); @@ -73,20 +82,29 @@ cygheap_setup_for_child (child_info *ci) ci->cygheap_h = CreateFileMapping (INVALID_HANDLE_VALUE, &sec_none, CFMAP_OPTIONS, 0, CYGHEAPSIZE, NULL); newcygheap = MapViewOfFileEx (ci->cygheap_h, MVMAP_OPTIONS, 0, 0, 0, NULL); - if (!VirtualAlloc (newcygheap, n, MEM_COMMIT, PAGE_READWRITE)) - api_fatal ("couldn't allocate new cygwin heap for child, %E"); - memcpy (newcygheap, cygheap, n); - UnmapViewOfFile (newcygheap); - ci->cygheap = cygheap; - ci->cygheap_max = cygheap_max; ProtectHandle1 (ci->cygheap_h, passed_cygheap_h); + if (!dup_later) + dup_now (newcygheap, ci, n); cygheap_protect->release (); - return; + ci->cygheap = cygheap; + ci->cygheap_max = cygheap_max; + return newcygheap; } void __stdcall -cygheap_setup_for_child_cleanup (child_info *ci) +cygheap_setup_for_child_cleanup (void *newcygheap, child_info *ci, + bool dup_it_now) { + if (dup_it_now) + { + /* NOTE: There is an assumption here that cygheap_max has not changed + between the time that cygheap_setup_for_child was called and now. + Make sure that this is a correct assumption. */ + cygheap_protect->acquire (); + dup_now (newcygheap, ci, (char *) cygheap_max - (char *) cygheap); + cygheap_protect->release (); + } + UnmapViewOfFile (newcygheap); ForceCloseHandle1 (ci->cygheap_h, passed_cygheap_h); } diff --git a/winsup/cygwin/cygheap.h b/winsup/cygwin/cygheap.h index 99aea8da9..1972411c7 100644 --- a/winsup/cygwin/cygheap.h +++ b/winsup/cygwin/cygheap.h @@ -177,8 +177,8 @@ extern init_cygheap *cygheap; extern void *cygheap_max; class child_info; -void __stdcall cygheap_setup_for_child (child_info *ci) __attribute__ ((regparm(1))); -void __stdcall cygheap_setup_for_child_cleanup (child_info *ci) __attribute__ ((regparm(1))); +void *__stdcall cygheap_setup_for_child (child_info *ci, bool dup_later) __attribute__ ((regparm(2))); +void __stdcall cygheap_setup_for_child_cleanup (void *, child_info *, bool) __attribute__ ((regparm(3))); void __stdcall cygheap_fixup_in_child (child_info *, bool); extern "C" { void __stdcall cfree (void *) __attribute__ ((regparm(1))); diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 3998cb56b..83e25393f 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -464,7 +464,8 @@ fork_parent (HANDLE& hParent, dll *&first_dll, syscall_printf ("CreateProcess (%s, %s, 0, 0, 1, %x, 0, 0, %p, %p)", myself->progname, myself->progname, c_flags, &si, &pi); __malloc_lock (_reent_clib ()); - cygheap_setup_for_child (&ch); + void *newheap; + newheap = cygheap_setup_for_child (&ch,cygheap->fdtab.need_fixup_before ()); rc = CreateProcess (myself->progname, /* image to run */ myself->progname, /* what we send in arg0 */ allow_ntsec ? sec_user (sa_buf) : &sec_none_nih, @@ -477,7 +478,6 @@ fork_parent (HANDLE& hParent, dll *&first_dll, &pi); CloseHandle (hParent); - cygheap_setup_for_child_cleanup (&ch); if (!rc) { @@ -489,14 +489,18 @@ fork_parent (HANDLE& hParent, dll *&first_dll, if (cygheap->user.impersonated && cygheap->user.token != INVALID_HANDLE_VALUE) ImpersonateLoggedOnUser (cygheap->user.token); + cygheap_setup_for_child_cleanup (newheap, &ch, 0); return -1; } /* Fixup the parent datastructure if needed and resume the child's main thread. */ - if (cygheap->fdtab.need_fixup_before ()) + if (!cygheap->fdtab.need_fixup_before ()) + cygheap_setup_for_child_cleanup (newheap, &ch, 0); + else { cygheap->fdtab.fixup_before_fork (pi.dwProcessId); + cygheap_setup_for_child_cleanup (newheap, &ch, 1); ResumeThread (pi.hThread); } @@ -718,6 +722,12 @@ vfork () cygheap->fdtab.vfork_parent_restore (); vf = get_vfork_val (); + + __asm__ volatile ("movl %%esp,%0": "=r" (esp):); + for (pp = (char **)vf->frame, esp = vf->vfork_esp; + esp <= vf->vfork_ebp + 1; pp++, esp++) + *esp = *pp; + if (vf->pid < 0) { int exitval = -vf->pid; @@ -725,11 +735,6 @@ vfork () exit (exitval); } - __asm__ volatile ("movl %%esp,%0": "=r" (esp):); - for (pp = (char **)vf->frame, esp = vf->vfork_esp; - esp <= vf->vfork_ebp + 1; pp++, esp++) - *esp = *pp; - return vf->pid; #endif } diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc index 1e443cb30..248dd426e 100644 --- a/winsup/cygwin/spawn.cc +++ b/winsup/cygwin/spawn.cc @@ -590,6 +590,7 @@ spawn_guts (HANDLE hToken, const char * prog_arg, const char *const *argv, syscall_printf ("spawn_guts null_app_name %d (%s, %.132s)", null_app_name, runpath, one_line.buf); + void *newheap; cygbench ("spawn-guts"); if (!hToken) { @@ -597,7 +598,7 @@ spawn_guts (HANDLE hToken, const char * prog_arg, const char *const *argv, /* FIXME: This leaks a handle in the CreateProcessAsUser case since the child process doesn't know about cygwin_mount_h. */ ciresrv.mount_h = cygwin_mount_h; - cygheap_setup_for_child (&ciresrv); + newheap = cygheap_setup_for_child (&ciresrv, cygheap->fdtab.need_fixup_before ()); rc = CreateProcess (runpath, /* image name - with full path */ one_line.buf, /* what was passed to exec */ /* process security attrs */ @@ -659,7 +660,7 @@ spawn_guts (HANDLE hToken, const char * prog_arg, const char *const *argv, strcat (wstname, dskname); si.lpDesktop = wstname; - cygheap_setup_for_child (&ciresrv); + newheap = cygheap_setup_for_child (&ciresrv, cygheap->fdtab.need_fixup_before ()); rc = CreateProcessAsUser (hToken, runpath, /* image name - with full path */ one_line.buf, /* what was passed to exec */ @@ -682,7 +683,6 @@ spawn_guts (HANDLE hToken, const char * prog_arg, const char *const *argv, MALLOC_CHECK; if (envblock) free (envblock); - cygheap_setup_for_child_cleanup (&ciresrv); MALLOC_CHECK; /* Set errno now so that debugging messages from it appear before our @@ -694,14 +694,18 @@ spawn_guts (HANDLE hToken, const char * prog_arg, const char *const *argv, syscall_printf ("CreateProcess failed, %E"); if (spr) ForceCloseHandle (spr); + cygheap_setup_for_child_cleanup (newheap, &ciresrv, 0); return -1; } /* Fixup the parent datastructure if needed and resume the child's main thread. */ - if (cygheap->fdtab.need_fixup_before ()) + if (!cygheap->fdtab.need_fixup_before ()) + cygheap_setup_for_child_cleanup (newheap, &ciresrv, 0); + else { cygheap->fdtab.fixup_before_exec (pi.dwProcessId); + cygheap_setup_for_child_cleanup (newheap, &ciresrv, 1); if (mode == _P_OVERLAY) ResumeThread (pi.hThread); } -- 2.43.5