forkables: On fork failure, retry with hardlinks.

To support in-cygwin package managers, the fork() implementation must
not rely on .exe and .dll files to stay in their original location, as
the package manager's job is to replace these files.  Instead, when the
first fork try fails, and we have NTFS, we use hardlinks to the original
binaries in /var/run/cygfork/ to create the child process during the
second fork try, along the main.exe.local file to enable the "DotLocal
Dll Redirection" feature for the dlls.

The (probably few) users that need an update-safe fork manually have to
create the /var/run/cygfork/ directory for now, using:
mkdir --mode=a=rwxt /var/run/cygfork

	* child_info.h: Bump CURR_CHILD_INFO_MAGIC.
	(enum child_status): Add _CI_SILENTFAIL flag.
	(struct child_info): Add silentfail setter and getter.
	* winsup.h (child_copy): Add bool silentfail parameter.
	* cygheap.cc: Pass silentfail parameter to child_copy.
	* dcrt0.cc: Ditto.
	* dll_init.h (struct dll): Define public inline method forkedntname.
	(struct dll_list): Declare private method find_by_forkedntname.
	* dll_init.cc (struct dll_list): Implement find_by_forkedntname.
	(dll_list::alloc): Use find_by_forkedntname when in load after fork.
	(dll_list::load_after_fork_impl): Load dlls using dll::forkedntname.
	* fork.cc (frok::parent): Set silentfail child info flag.  Pass
	silentfail parameter to child_copy.  Use forkedntname of
	dlls.main_executable.
	(fork): When first dofork run failed and did not use forkables,
	run dofork again with_forkables set to true.
	(child_copy): Use debug_printf if silentfail is true,
	system_printf otherwise.
This commit is contained in:
Michael Haubenwallner
2016-12-07 11:58:28 +01:00
committed by Corinna Vinschen
parent 8ddb1f60c8
commit ece7282f32
8 changed files with 85 additions and 33 deletions

View File

@@ -289,6 +289,19 @@ dll_list::find_by_modname (PCWCHAR modname)
return NULL;
}
/* Look for a dll based on the ntname used
to dynamically reload in forked child. */
dll *
dll_list::find_by_forkedntname (PCWCHAR ntname)
{
dll *d = &start;
while ((d = d->next) != NULL)
if (!wcscasecmp (ntname, d->forkedntname ()))
return d;
return NULL;
}
#define RETRIES 1000
/* Allocate space for a dll struct. */
@@ -308,8 +321,11 @@ dll_list::alloc (HINSTANCE h, per_process *p, dll_type type)
/* Already loaded? For linked DLLs, only compare the basenames. Linked
DLLs are loaded using just the basename and the default DLL search path.
The Windows loader picks up the first one it finds.
This also applies to cygwin1.dll and the main-executable (DLL_SELF). */
dll *d = (type != DLL_LOAD) ? dlls.find_by_modname (modname) : dlls[ntname];
This also applies to cygwin1.dll and the main-executable (DLL_SELF).
When in_load_after_fork, dynamically loaded dll's are reloaded
using their parent's forkable_ntname, if available. */
dll *d = (type != DLL_LOAD) ? dlls.find_by_modname (modname) :
in_load_after_fork ? dlls.find_by_forkedntname (ntname) : dlls[ntname];
if (d)
{
/* We only get here in the forkee. */
@@ -714,14 +730,16 @@ void dll_list::load_after_fork_impl (HANDLE parent, dll* d, int retries)
fabort ("unable to release protective reservation (%p) for %W, %E",
d->handle, d->ntname);
HMODULE h = LoadLibraryExW (buffered_shortname (d->ntname),
HMODULE h = LoadLibraryExW (buffered_shortname (d->forkedntname ()),
NULL, DONT_RESOLVE_DLL_REFERENCES);
if (!h)
fabort ("unable to create interim mapping for %W, %E", d->ntname);
fabort ("unable to create interim mapping for %W (using %W), %E",
d->ntname, buffered_shortname (d->forkedntname ()));
if (h != d->handle)
{
sigproc_printf ("%W loaded in wrong place: %p != %p",
d->ntname, h, d->handle);
sigproc_printf ("%W (using %W) loaded in wrong place: %p != %p",
d->ntname, buffered_shortname (d->forkedntname ()),
h, d->handle);
FreeLibrary (h);
PVOID reservation = reserve_at (d->ntname, h,
d->handle, d->image_size);
@@ -732,8 +750,8 @@ void dll_list::load_after_fork_impl (HANDLE parent, dll* d, int retries)
if (retries < DLL_RETRY_MAX)
load_after_fork_impl (parent, d, retries+1);
else
fabort ("unable to remap %W to same address as parent (%p) - try running rebaseall",
d->ntname, d->handle);
fabort ("unable to remap %W (using %W) to same address as parent (%p) - try running rebaseall",
d->ntname, buffered_shortname (d->forkedntname ()), d->handle);
/* once the above returns all the dlls are mapped; release
the reservation and continue unwinding */
@@ -761,20 +779,21 @@ void dll_list::load_after_fork_impl (HANDLE parent, dll* d, int retries)
/* Free the library using our parent's handle: it's identical
to ours or we wouldn't have gotten this far */
if (!FreeLibrary (d->handle))
fabort ("unable to unload interim mapping of %W, %E",
d->ntname);
fabort ("unable to unload interim mapping of %W (using %W), %E",
d->ntname, buffered_shortname (d->forkedntname ()));
}
/* cygwin1.dll - as linked dependency - may reuse the shortname
buffer, even in case of failure: don't reuse shortname later */
HMODULE h = LoadLibraryW (buffered_shortname (d->ntname));
HMODULE h = LoadLibraryW (buffered_shortname (d->forkedntname ()));
if (!h)
fabort ("unable to map %W, %E", d->ntname);
fabort ("unable to map %W (using %W), %E",
d->ntname, buffered_shortname (d->forkedntname ()));
if (h != d->handle)
fabort ("unable to map %W to same address as parent: %p != %p",
d->ntname, d->handle, h);
fabort ("unable to map %W (using %W) to same address as parent: %p != %p",
d->ntname, buffered_shortname (d->forkedntname ()), d->handle, h);
/* Fix OS reference count. */
for (int cnt = 1; cnt < d->count; ++cnt)
LoadLibraryW (buffered_shortname (d->ntname));
LoadLibraryW (buffered_shortname (d->forkedntname ()));
}
}