2000-02-18 03:38:33 +08:00
|
|
|
/* fork.cc
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2012-01-12 03:07:11 +08:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2000-09-02 04:54:22 +08:00
|
|
|
#include "child_info.h"
|
2004-11-26 12:15:10 +08:00
|
|
|
#include "cygtls.h"
|
2008-03-07 19:24:51 +08:00
|
|
|
#include "tls_pbuf.h"
|
2017-03-03 00:26:53 +08:00
|
|
|
#include "shared_info.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "dll_init.h"
|
2002-08-17 03:41:39 +08:00
|
|
|
#include "cygmalloc.h"
|
2011-05-20 15:23:11 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-10-29 01:34:29 +08:00
|
|
|
#define NPIDS_HELD 4
|
2004-09-15 07:44:50 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Timeout to wait for child to start, parent to init child, etc. */
|
|
|
|
/* FIXME: Once things stabilize, bump up to a few minutes. */
|
|
|
|
#define FORK_WAIT_TIMEOUT (300 * 1000) /* 300 seconds */
|
|
|
|
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
static int dofork (bool *with_forkables);
|
2005-09-28 23:18:49 +08:00
|
|
|
class frok
|
|
|
|
{
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
frok (bool *forkables)
|
|
|
|
: with_forkables (forkables)
|
|
|
|
{}
|
|
|
|
bool *with_forkables;
|
2005-09-28 23:18:49 +08:00
|
|
|
bool load_dlls;
|
|
|
|
child_info_fork ch;
|
2011-05-29 04:09:04 +08:00
|
|
|
const char *errmsg;
|
2005-09-28 23:18:49 +08:00
|
|
|
int child_pid;
|
|
|
|
int this_errno;
|
2011-05-29 04:09:04 +08:00
|
|
|
HANDLE hchild;
|
2007-02-22 20:34:55 +08:00
|
|
|
int __stdcall parent (volatile char * volatile here);
|
|
|
|
int __stdcall child (volatile char * volatile here);
|
2011-05-29 04:09:04 +08:00
|
|
|
bool error (const char *fmt, ...);
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
friend int dofork (bool *with_forkables);
|
2005-09-28 23:18:49 +08:00
|
|
|
};
|
|
|
|
|
2006-03-14 02:29:48 +08:00
|
|
|
static void
|
2004-12-06 03:41:26 +08:00
|
|
|
resume_child (HANDLE forker_finished)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
SetEvent (forker_finished);
|
2000-10-13 06:15:47 +08:00
|
|
|
debug_printf ("signalled child");
|
2006-03-14 02:29:48 +08:00
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-04-15 01:08:02 +08:00
|
|
|
/* Notify parent that it is time for the next step. */
|
2000-10-13 06:15:47 +08:00
|
|
|
static void __stdcall
|
2002-09-22 11:38:57 +08:00
|
|
|
sync_with_parent (const char *s, bool hang_self)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-13 06:15:47 +08:00
|
|
|
debug_printf ("signalling parent: %s", s);
|
2004-12-06 03:41:26 +08:00
|
|
|
fork_info->ready (false);
|
2000-10-13 06:15:47 +08:00
|
|
|
if (hang_self)
|
|
|
|
{
|
2002-06-16 05:59:32 +08:00
|
|
|
HANDLE h = fork_info->forker_finished;
|
2000-10-13 06:15:47 +08:00
|
|
|
/* Wait for the parent to fill in our stack and heap.
|
|
|
|
Don't wait forever here. If our parent dies we don't want to clog
|
|
|
|
the system. If the wait fails, we really can't continue so exit. */
|
|
|
|
DWORD psync_rc = WaitForSingleObject (h, FORK_WAIT_TIMEOUT);
|
|
|
|
debug_printf ("awake");
|
|
|
|
switch (psync_rc)
|
|
|
|
{
|
|
|
|
case WAIT_TIMEOUT:
|
2004-02-12 11:01:58 +08:00
|
|
|
api_fatal ("WFSO timed out %s", s);
|
2000-10-13 06:15:47 +08:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
|
|
|
if (GetLastError () == ERROR_INVALID_HANDLE &&
|
2002-06-16 05:59:32 +08:00
|
|
|
WaitForSingleObject (fork_info->forker_finished, 1) != WAIT_FAILED)
|
2000-10-13 06:15:47 +08:00
|
|
|
break;
|
2004-02-12 11:01:58 +08:00
|
|
|
api_fatal ("WFSO failed %s, fork_finished %p, %E", s,
|
2002-06-16 05:59:32 +08:00
|
|
|
fork_info->forker_finished);
|
2000-10-13 06:15:47 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_printf ("no problems");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-29 04:09:04 +08:00
|
|
|
bool
|
|
|
|
frok::error (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
DWORD exit_code = ch.exit_code;
|
|
|
|
if (!exit_code && hchild)
|
|
|
|
{
|
|
|
|
exit_code = ch.proc_retry (hchild);
|
|
|
|
if (!exit_code)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (exit_code != EXITCODE_FORK_FAILED)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
static char buf[NT_MAX_PATH + 256];
|
|
|
|
va_start (ap, fmt);
|
|
|
|
__small_vsprintf (buf, fmt, ap);
|
|
|
|
errmsg = buf;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-20 23:07:30 +08:00
|
|
|
/* Set up a pipe which will track the life of a "pid" through
|
|
|
|
even after we've exec'ed. */
|
|
|
|
void
|
|
|
|
child_info::prefork (bool detached)
|
|
|
|
{
|
|
|
|
if (!detached)
|
|
|
|
{
|
|
|
|
if (!CreatePipe (&rd_proc_pipe, &wr_proc_pipe, &sec_none_nih, 16))
|
2012-12-28 01:25:17 +08:00
|
|
|
api_fatal ("prefork: couldn't create pipe process tracker, %E");
|
2012-03-20 23:07:30 +08:00
|
|
|
|
|
|
|
if (!SetHandleInformation (wr_proc_pipe, HANDLE_FLAG_INHERIT,
|
|
|
|
HANDLE_FLAG_INHERIT))
|
|
|
|
api_fatal ("prefork: couldn't set process pipe(%p) inherit state, %E",
|
|
|
|
wr_proc_pipe);
|
|
|
|
ProtectHandle1 (rd_proc_pipe, rd_proc_pipe);
|
|
|
|
ProtectHandle1 (wr_proc_pipe, wr_proc_pipe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
int __stdcall
|
2007-02-22 20:34:55 +08:00
|
|
|
frok::child (volatile char * volatile here)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-09-28 23:18:49 +08:00
|
|
|
HANDLE& hParent = ch.parent;
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
sync_with_parent ("after longjmp", true);
|
2011-11-25 05:36:53 +08:00
|
|
|
debug_printf ("child is running. pid %d, ppid %d, stack here %p",
|
|
|
|
myself->pid, myself->ppid, __builtin_frame_address (0));
|
2009-06-08 11:53:40 +08:00
|
|
|
sigproc_printf ("hParent %p, load_dlls %d", hParent, load_dlls);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2017-03-11 03:44:53 +08:00
|
|
|
/* Make sure threadinfo information is properly set up. */
|
|
|
|
if (&_my_tls != _main_tls)
|
2004-02-12 11:01:58 +08:00
|
|
|
{
|
|
|
|
_main_tls = &_my_tls;
|
|
|
|
_main_tls->init_thread (NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2007-02-23 23:15:50 +08:00
|
|
|
set_cygwin_privileges (hProcToken);
|
|
|
|
clear_procimptoken ();
|
|
|
|
cygheap->user.reimpersonate ();
|
2005-04-03 21:06:43 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
#ifdef DEBUGGING
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("FORKDEBUG", NULL, 0))
|
2000-10-13 06:15:47 +08:00
|
|
|
try_to_debug ();
|
|
|
|
char buf[80];
|
|
|
|
/* This is useful for debugging fork problems. Use gdb to attach to
|
|
|
|
the pid reported here. */
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_FORK_SLEEP", buf, sizeof (buf)))
|
2000-10-13 06:15:47 +08:00
|
|
|
{
|
|
|
|
small_printf ("Sleeping %d after fork, pid %u\n", atoi (buf), GetCurrentProcessId ());
|
2002-09-22 11:38:57 +08:00
|
|
|
Sleep (atoi (buf));
|
2000-10-13 06:15:47 +08:00
|
|
|
}
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-04-01 19:47:00 +08:00
|
|
|
/* Incredible but true: If we use sockets and SYSV IPC shared memory,
|
|
|
|
there's a good chance that a duplicated socket in the child occupies
|
|
|
|
memory which is needed to duplicate shared memory from the parent
|
|
|
|
process, if the shared memory hasn't been duplicated already.
|
|
|
|
The same goes very likely for "normal" mmap shared memory, too, but
|
|
|
|
with SYSV IPC it was the first time observed. So, *never* fixup
|
|
|
|
fdtab before fixing up shared memory. */
|
|
|
|
if (fixup_shms_after_fork ())
|
|
|
|
api_fatal ("recreate_shm areas after fork failed");
|
2002-10-21 09:00:58 +08:00
|
|
|
|
2016-12-07 18:58:26 +08:00
|
|
|
/* load dynamic dlls, if any, re-track main-executable and cygwin1.dll */
|
2016-12-07 18:58:25 +08:00
|
|
|
dlls.load_after_fork (hParent);
|
|
|
|
|
|
|
|
cygheap->fdtab.fixup_after_fork (hParent);
|
|
|
|
|
2019-04-30 15:09:13 +08:00
|
|
|
/* Signal that we have successfully initialized, so the parent can
|
2019-06-11 20:11:01 +08:00
|
|
|
- transfer data/bss for dynamically loaded dlls (if any), or
|
2019-04-30 15:09:13 +08:00
|
|
|
- terminate the current fork call even if the child is initialized. */
|
|
|
|
sync_with_parent ("performed fork fixups and dynamic dll loading", true);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-29 02:17:09 +08:00
|
|
|
init_console_handler (myself->ctty > 0);
|
2005-07-07 04:05:03 +08:00
|
|
|
ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
|
2000-07-15 10:48:11 +08:00
|
|
|
|
2002-09-17 17:12:36 +08:00
|
|
|
pthread::atforkchild ();
|
2000-10-17 07:55:58 +08:00
|
|
|
cygbench ("fork-child");
|
2005-08-12 00:30:48 +08:00
|
|
|
ld_preload ();
|
2005-10-29 23:19:34 +08:00
|
|
|
fixup_hooks_after_fork ();
|
|
|
|
_my_tls.fixup_after_fork ();
|
2012-03-20 23:07:30 +08:00
|
|
|
/* Clear this or the destructor will close them. In the case of
|
|
|
|
rd_proc_pipe that would be an invalid handle. In the case of
|
|
|
|
wr_proc_pipe it would be == my_wr_proc_pipe. Both would be bad. */
|
|
|
|
ch.rd_proc_pipe = ch.wr_proc_pipe = NULL;
|
2019-01-27 20:15:15 +08:00
|
|
|
CloseHandle (hParent);
|
|
|
|
hParent = NULL;
|
2005-11-05 01:38:32 +08:00
|
|
|
cygwin_finished_initializing = true;
|
2000-10-13 06:15:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
int __stdcall
|
2007-02-22 20:34:55 +08:00
|
|
|
frok::parent (volatile char * volatile stack_here)
|
2000-10-13 06:15:47 +08:00
|
|
|
{
|
2004-12-06 03:41:26 +08:00
|
|
|
HANDLE forker_finished;
|
2000-10-13 06:15:47 +08:00
|
|
|
DWORD rc;
|
2005-09-28 23:18:49 +08:00
|
|
|
child_pid = -1;
|
|
|
|
this_errno = 0;
|
|
|
|
bool fix_impersonation = false;
|
|
|
|
pinfo child;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-12-19 04:32:04 +08:00
|
|
|
int c_flags = GetPriorityClass (GetCurrentProcess ());
|
2005-10-30 04:33:59 +08:00
|
|
|
debug_printf ("priority class %d", c_flags);
|
2013-05-24 16:43:43 +08:00
|
|
|
/* Per MSDN, this must be specified even if lpEnvironment is set to NULL,
|
|
|
|
otherwise UNICODE characters in the parent environment are not copied
|
|
|
|
correctly to the child. Omitting it may scramble %PATH% on non-English
|
|
|
|
systems. */
|
|
|
|
c_flags |= CREATE_UNICODE_ENVIRONMENT;
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2011-05-29 04:09:04 +08:00
|
|
|
errmsg = NULL;
|
|
|
|
hchild = NULL;
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* If we don't have a console, then don't create a console for the
|
|
|
|
child either. */
|
2002-09-19 23:12:48 +08:00
|
|
|
HANDLE console_handle = CreateFile ("CONOUT$", GENERIC_WRITE,
|
2011-07-04 23:25:36 +08:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
&sec_none_nih, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2002-09-19 23:12:48 +08:00
|
|
|
if (console_handle != INVALID_HANDLE_VALUE)
|
2000-10-13 06:15:47 +08:00
|
|
|
CloseHandle (console_handle);
|
|
|
|
else
|
|
|
|
c_flags |= DETACHED_PROCESS;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-11-17 18:43:01 +08:00
|
|
|
/* Some file types (currently only sockets) need extra effort in the
|
|
|
|
parent after CreateProcess and before copying the datastructures
|
|
|
|
to the child. So we have to start the child in suspend state,
|
|
|
|
unfortunately, to avoid a race condition. */
|
|
|
|
if (cygheap->fdtab.need_fixup_before ())
|
|
|
|
c_flags |= CREATE_SUSPENDED;
|
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
/* Remember if we need to load dynamically linked dlls.
|
|
|
|
We do this here so that this information will be available
|
|
|
|
in the parent and, when the stack is copied, in the child. */
|
2000-10-13 06:15:47 +08:00
|
|
|
load_dlls = dlls.reload_on_fork && dlls.loaded_dlls;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
forker_finished = CreateEvent (&sec_all, FALSE, FALSE, NULL);
|
|
|
|
if (forker_finished == NULL)
|
|
|
|
{
|
2005-09-28 23:18:49 +08:00
|
|
|
this_errno = geterrno_from_win_error ();
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("unable to allocate forker_finished event");
|
2000-10-13 06:15:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-07-14 04:00:27 +08:00
|
|
|
ProtectHandleINH (forker_finished);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
ch.forker_finished = forker_finished;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2015-12-03 19:59:56 +08:00
|
|
|
ch.stackbase = NtCurrentTeb ()->Tib.StackBase;
|
2015-07-07 23:05:20 +08:00
|
|
|
ch.stackaddr = NtCurrentTeb ()->DeallocationStack;
|
2015-07-01 21:28:16 +08:00
|
|
|
if (!ch.stackaddr)
|
2015-07-07 23:05:20 +08:00
|
|
|
{
|
|
|
|
/* If DeallocationStack is NULL, we're running on an application-provided
|
|
|
|
stack. If so, the entire stack is committed anyway and StackLimit
|
|
|
|
points to the allocation address of the stack. Mark in guardsize that
|
|
|
|
we must not set up guard pages. */
|
2015-12-03 19:59:56 +08:00
|
|
|
ch.stackaddr = ch.stacklimit = NtCurrentTeb ()->Tib.StackLimit;
|
2015-07-07 23:05:20 +08:00
|
|
|
ch.guardsize = (size_t) -1;
|
|
|
|
}
|
2015-07-01 21:28:16 +08:00
|
|
|
else
|
2011-05-20 15:23:11 +08:00
|
|
|
{
|
2015-07-07 23:05:20 +08:00
|
|
|
/* Otherwise we're running on a system-allocated stack. Since stack_here
|
|
|
|
is the address of the stack pointer we start the child with anyway, we
|
2015-12-02 19:31:40 +08:00
|
|
|
can set ch.stacklimit to this value rounded down to page size. The
|
2015-07-07 23:05:20 +08:00
|
|
|
child will not need the rest of the stack anyway. Guardsize depends
|
|
|
|
on whether we're running on a pthread or not. If pthread, we fetch
|
|
|
|
the guardpage size from the pthread attribs, otherwise we use the
|
|
|
|
system default. */
|
2018-05-26 00:27:21 +08:00
|
|
|
ch.stacklimit = (void *) ((uintptr_t) stack_here & ~(wincap.page_size () - 1));
|
2015-07-07 23:05:20 +08:00
|
|
|
ch.guardsize = (&_my_tls != _main_tls && _my_tls.tid)
|
|
|
|
? _my_tls.tid->attr.guardsize
|
|
|
|
: wincap.def_guard_page_size ();
|
2011-05-20 15:23:11 +08:00
|
|
|
}
|
2013-05-24 04:10:35 +08:00
|
|
|
debug_printf ("stack - bottom %p, top %p, addr %p, guardsize %ly",
|
2015-12-02 19:31:40 +08:00
|
|
|
ch.stackbase, ch.stacklimit, ch.stackaddr, ch.guardsize);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-03-14 02:29:48 +08:00
|
|
|
PROCESS_INFORMATION pi;
|
2008-03-07 19:24:51 +08:00
|
|
|
STARTUPINFOW si;
|
2006-03-14 02:29:48 +08:00
|
|
|
|
|
|
|
memset (&si, 0, sizeof (si));
|
2008-03-07 19:24:51 +08:00
|
|
|
si.cb = sizeof si;
|
2006-12-05 18:59:21 +08:00
|
|
|
|
2004-12-06 03:41:26 +08:00
|
|
|
si.lpReserved2 = (LPBYTE) &ch;
|
2002-09-22 11:38:57 +08:00
|
|
|
si.cbReserved2 = sizeof (ch);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-03-29 14:35:25 +08:00
|
|
|
bool locked = __malloc_lock ();
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* Remove impersonation */
|
2003-06-30 21:07:36 +08:00
|
|
|
cygheap->user.deimpersonate ();
|
2005-09-28 23:18:49 +08:00
|
|
|
fix_impersonation = true;
|
2011-05-30 14:52:12 +08:00
|
|
|
ch.refresh_cygheap ();
|
2012-03-20 23:07:30 +08:00
|
|
|
ch.prefork (); /* set up process tracking pipes. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
*with_forkables = dlls.setup_forkables (*with_forkables);
|
|
|
|
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
ch.silentfail (!*with_forkables); /* fail silently without forkables */
|
|
|
|
|
2019-03-12 18:41:35 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PSECURITY_ATTRIBUTES sa = (PSECURITY_ATTRIBUTES) tp.w_get ();
|
|
|
|
if (!sec_user_nih (sa, cygheap->user.saved_sid (),
|
|
|
|
well_known_authenticated_users_sid,
|
|
|
|
PROCESS_QUERY_LIMITED_INFORMATION))
|
|
|
|
sa = &sec_none_nih;
|
|
|
|
|
2006-03-14 05:10:14 +08:00
|
|
|
while (1)
|
2000-10-13 06:15:47 +08:00
|
|
|
{
|
2016-12-07 18:58:26 +08:00
|
|
|
PCWCHAR forking_progname = NULL;
|
|
|
|
if (dlls.main_executable)
|
|
|
|
forking_progname = dll_list::buffered_shortname
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
(dlls.main_executable->forkedntname ());
|
2016-12-07 18:58:26 +08:00
|
|
|
if (!forking_progname || !*forking_progname)
|
|
|
|
forking_progname = myself->progname;
|
|
|
|
|
2016-12-07 18:58:25 +08:00
|
|
|
syscall_printf ("CreateProcessW (%W, %W, 0, 0, 1, %y, 0, 0, %p, %p)",
|
2016-12-07 18:58:26 +08:00
|
|
|
forking_progname, myself->progname, c_flags, &si, &pi);
|
2016-12-07 18:58:25 +08:00
|
|
|
|
2011-05-29 04:09:04 +08:00
|
|
|
hchild = NULL;
|
2016-12-07 18:58:26 +08:00
|
|
|
/* cygwin1.dll may reuse the forking_progname buffer, even
|
|
|
|
in case of failure: don't reuse forking_progname later */
|
|
|
|
rc = CreateProcessW (forking_progname, /* image to run */
|
2013-05-24 04:47:45 +08:00
|
|
|
GetCommandLineW (), /* Take same space for command
|
|
|
|
line as in parent to make
|
|
|
|
sure child stack is allocated
|
|
|
|
in the same memory location
|
|
|
|
as in parent. */
|
2019-03-12 18:41:35 +08:00
|
|
|
sa,
|
|
|
|
sa,
|
|
|
|
TRUE, /* inherit handles */
|
2008-03-07 19:24:51 +08:00
|
|
|
c_flags,
|
2019-03-12 18:41:35 +08:00
|
|
|
NULL, /* environ filled in later */
|
|
|
|
0, /* use cwd */
|
2008-03-07 19:24:51 +08:00
|
|
|
&si,
|
|
|
|
&pi);
|
2006-03-14 05:10:14 +08:00
|
|
|
|
2011-11-14 09:29:49 +08:00
|
|
|
if (rc)
|
|
|
|
debug_printf ("forked pid %u", pi.dwProcessId);
|
|
|
|
else
|
2006-03-14 05:10:14 +08:00
|
|
|
{
|
|
|
|
this_errno = geterrno_from_win_error ();
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("CreateProcessW failed for '%W'", myself->progname);
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
dlls.release_forkables ();
|
2006-03-14 05:10:14 +08:00
|
|
|
memset (&pi, 0, sizeof (pi));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-06-17 03:36:07 +08:00
|
|
|
|
2009-11-17 18:43:01 +08:00
|
|
|
if (cygheap->fdtab.need_fixup_before ())
|
|
|
|
{
|
|
|
|
cygheap->fdtab.fixup_before_fork (pi.dwProcessId);
|
|
|
|
ResumeThread (pi.hThread);
|
|
|
|
}
|
|
|
|
|
2006-03-19 03:17:21 +08:00
|
|
|
CloseHandle (pi.hThread);
|
2011-05-29 04:09:04 +08:00
|
|
|
hchild = pi.hProcess;
|
2006-03-19 03:17:21 +08:00
|
|
|
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
dlls.release_forkables ();
|
|
|
|
|
2006-03-19 03:17:21 +08:00
|
|
|
/* Protect the handle but name it similarly to the way it will
|
|
|
|
be called in subproc handling. */
|
2011-05-29 04:09:04 +08:00
|
|
|
ProtectHandle1 (hchild, childhProc);
|
2006-03-19 03:17:21 +08:00
|
|
|
|
2011-11-25 05:36:53 +08:00
|
|
|
strace.write_childpid (pi.dwProcessId);
|
2006-03-14 05:10:14 +08:00
|
|
|
|
|
|
|
/* Wait for subproc to initialize itself. */
|
2011-05-29 04:09:04 +08:00
|
|
|
if (!ch.sync (pi.dwProcessId, hchild, FORK_WAIT_TIMEOUT))
|
2006-03-14 05:10:14 +08:00
|
|
|
{
|
2013-06-28 01:00:12 +08:00
|
|
|
if (!error ("forked process %u died unexpectedly, retry %d, exit code %y",
|
2012-05-08 23:06:43 +08:00
|
|
|
pi.dwProcessId, ch.retry, ch.exit_code))
|
2006-03-15 08:29:14 +08:00
|
|
|
continue;
|
2006-03-14 05:10:14 +08:00
|
|
|
this_errno = EAGAIN;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-12-30 04:46:34 +08:00
|
|
|
|
2006-03-29 14:35:25 +08:00
|
|
|
/* Restore impersonation */
|
|
|
|
cygheap->user.reimpersonate ();
|
|
|
|
fix_impersonation = false;
|
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
child_pid = cygwin_pid (pi.dwProcessId);
|
2013-12-18 11:58:11 +08:00
|
|
|
child.init (child_pid, PID_IN_USE | PID_NEW, NULL);
|
2004-08-31 11:34:04 +08:00
|
|
|
|
2004-12-06 03:41:26 +08:00
|
|
|
if (!child)
|
2003-09-26 11:20:30 +08:00
|
|
|
{
|
2005-09-28 23:18:49 +08:00
|
|
|
this_errno = get_errno () == ENOMEM ? ENOMEM : EAGAIN;
|
2003-09-26 11:20:30 +08:00
|
|
|
syscall_printf ("pinfo failed");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-07-29 22:22:56 +08:00
|
|
|
child->nice = myself->nice;
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* Initialize things that are done later in dll_crt0_1 that aren't done
|
|
|
|
for the forkee. */
|
2010-05-18 22:30:51 +08:00
|
|
|
wcscpy (child->progname, myself->progname);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* Fill in fields in the child's process table entry. */
|
2004-12-06 03:41:26 +08:00
|
|
|
child->dwProcessId = pi.dwProcessId;
|
2011-05-29 04:09:04 +08:00
|
|
|
child.hProcess = hchild;
|
2012-03-21 23:54:50 +08:00
|
|
|
ch.postfork (child);
|
2001-02-10 12:20:52 +08:00
|
|
|
|
2019-06-11 20:11:01 +08:00
|
|
|
/* Hopefully, this will succeed. The alternative to doing things this
|
|
|
|
way is to reserve space prior to calling CreateProcess and then fill
|
|
|
|
it in afterwards. This requires more bookkeeping than I like, though,
|
|
|
|
so we'll just do it the easy way. So, terminate any child process if
|
|
|
|
we can't actually record the pid in the internal table. */
|
|
|
|
if (!child.remember (false))
|
|
|
|
{
|
|
|
|
this_errno = EAGAIN;
|
|
|
|
#ifdef DEBUGGING0
|
|
|
|
error ("child remember failed");
|
|
|
|
#endif
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* CHILD IS STOPPED */
|
|
|
|
debug_printf ("child is alive (but stopped)");
|
|
|
|
|
2011-11-25 05:36:53 +08:00
|
|
|
|
2006-03-29 14:35:25 +08:00
|
|
|
/* Initialize, in order: stack, dll data, dll bss.
|
|
|
|
data, bss, heap were done earlier (in dcrt0.cc)
|
|
|
|
Note: variables marked as NO_COPY will not be copied since they are
|
|
|
|
placed in a protected segment. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-12-17 12:36:39 +08:00
|
|
|
const void *impure_beg;
|
|
|
|
const void *impure_end;
|
|
|
|
const char *impure;
|
2003-12-24 00:26:31 +08:00
|
|
|
if (&_my_tls == _main_tls)
|
2005-12-17 12:36:39 +08:00
|
|
|
impure_beg = impure_end = impure = NULL;
|
2003-12-24 00:26:31 +08:00
|
|
|
else
|
|
|
|
{
|
2005-12-17 12:36:39 +08:00
|
|
|
impure = "impure";
|
2003-12-24 00:26:31 +08:00
|
|
|
impure_beg = _impure_ptr;
|
|
|
|
impure_end = _impure_ptr + 1;
|
|
|
|
}
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
rc = child_copy (hchild, true, !*with_forkables,
|
2015-12-02 19:31:40 +08:00
|
|
|
"stack", stack_here, ch.stackbase,
|
2005-12-17 12:36:39 +08:00
|
|
|
impure, impure_beg, impure_end,
|
|
|
|
NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-17 03:41:39 +08:00
|
|
|
__malloc_unlock ();
|
2004-02-12 11:01:58 +08:00
|
|
|
locked = false;
|
2000-10-13 06:15:47 +08:00
|
|
|
if (!rc)
|
2006-03-14 02:29:48 +08:00
|
|
|
{
|
|
|
|
this_errno = get_errno ();
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("pid %u, exitval %p", pi.dwProcessId, ch.exit_code);
|
2006-03-14 02:29:48 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* Now fill data/bss of any DLLs that were linked into the program. */
|
|
|
|
for (dll *d = dlls.istart (DLL_LINK); d; d = dlls.inext ())
|
|
|
|
{
|
|
|
|
debug_printf ("copying data/bss of a linked dll");
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
if (!child_copy (hchild, true, !*with_forkables,
|
2005-12-17 12:36:39 +08:00
|
|
|
"linked dll data", d->p.data_start, d->p.data_end,
|
|
|
|
"linked dll bss", d->p.bss_start, d->p.bss_end,
|
|
|
|
NULL))
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
|
|
|
this_errno = get_errno ();
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("couldn't copy linked dll data/bss");
|
2005-09-28 23:18:49 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:13 +08:00
|
|
|
/* Start the child up, and then wait for it to
|
|
|
|
perform fork fixups and dynamic dll loading (if any). */
|
2006-03-14 02:29:48 +08:00
|
|
|
resume_child (forker_finished);
|
2011-05-29 04:09:04 +08:00
|
|
|
if (!ch.sync (child->pid, hchild, FORK_WAIT_TIMEOUT))
|
2004-12-06 03:41:26 +08:00
|
|
|
{
|
2005-09-28 23:18:49 +08:00
|
|
|
this_errno = EAGAIN;
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("died waiting for dll loading");
|
2004-12-06 03:41:26 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
/* If DLLs were loaded in the parent, then the child has reloaded all
|
|
|
|
of them and is now waiting to have all of the individual data and
|
|
|
|
bss sections filled in. */
|
|
|
|
if (load_dlls)
|
|
|
|
{
|
|
|
|
/* CHILD IS STOPPED */
|
|
|
|
/* write memory of reloaded dlls */
|
|
|
|
for (dll *d = dlls.istart (DLL_LOAD); d; d = dlls.inext ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-13 06:15:47 +08:00
|
|
|
debug_printf ("copying data/bss for a loaded dll");
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
if (!child_copy (hchild, true, !*with_forkables,
|
2005-12-17 12:36:39 +08:00
|
|
|
"loaded dll data", d->p.data_start, d->p.data_end,
|
|
|
|
"loaded dll bss", d->p.bss_start, d->p.bss_end,
|
|
|
|
NULL))
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
|
|
|
this_errno = get_errno ();
|
|
|
|
#ifdef DEBUGGING
|
2011-05-29 04:09:04 +08:00
|
|
|
error ("copying data/bss for a loaded dll");
|
2005-09-28 23:18:49 +08:00
|
|
|
#endif
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 18:35:33 +08:00
|
|
|
/* Do not attach to the child before it has successfully initialized.
|
|
|
|
Otherwise we may wait forever, or deliver an orphan SIGCHILD. */
|
|
|
|
if (!child.reattach ())
|
|
|
|
{
|
|
|
|
this_errno = EAGAIN;
|
|
|
|
#ifdef DEBUGGING0
|
|
|
|
error ("child reattach failed");
|
|
|
|
#endif
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:13 +08:00
|
|
|
/* Finally start the child up. */
|
|
|
|
resume_child (forker_finished);
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
ForceCloseHandle (forker_finished);
|
|
|
|
forker_finished = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-12-06 03:41:26 +08:00
|
|
|
return child_pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Common cleanup code for failure cases */
|
2005-09-28 23:18:49 +08:00
|
|
|
cleanup:
|
2018-06-05 18:40:21 +08:00
|
|
|
/* release procinfo before hProcess in destructor */
|
|
|
|
child.allow_remove ();
|
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
if (fix_impersonation)
|
|
|
|
cygheap->user.reimpersonate ();
|
2004-02-12 11:01:58 +08:00
|
|
|
if (locked)
|
|
|
|
__malloc_unlock ();
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Remember to de-allocate the fd table. */
|
2019-02-05 19:52:13 +08:00
|
|
|
if (hchild)
|
|
|
|
{
|
|
|
|
TerminateProcess (hchild, 1);
|
|
|
|
if (!child.hProcess) /* no child.procinfo */
|
|
|
|
ForceCloseHandle1 (hchild, childhProc);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
if (forker_finished)
|
|
|
|
ForceCloseHandle (forker_finished);
|
2005-09-28 23:18:49 +08:00
|
|
|
debug_printf ("returning -1");
|
2000-02-18 03:38:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-10-13 06:15:47 +08:00
|
|
|
extern "C" int
|
|
|
|
fork ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
bool with_forkables = false; /* do not force hardlinks on first try */
|
|
|
|
int res = dofork (&with_forkables);
|
|
|
|
if (res >= 0)
|
|
|
|
return res;
|
|
|
|
if (with_forkables)
|
|
|
|
return res; /* no need for second try when already enabled */
|
|
|
|
with_forkables = true; /* enable hardlinks for second try */
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
return dofork (&with_forkables);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dofork (bool *with_forkables)
|
|
|
|
{
|
|
|
|
frok grouped (with_forkables);
|
2000-10-13 06:15:47 +08:00
|
|
|
|
|
|
|
debug_printf ("entering");
|
2000-10-14 13:52:38 +08:00
|
|
|
grouped.load_dlls = 0;
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2005-10-29 23:19:34 +08:00
|
|
|
int res;
|
2007-02-22 20:34:55 +08:00
|
|
|
bool ischild = false;
|
2005-10-29 23:19:34 +08:00
|
|
|
|
2000-10-25 11:54:50 +08:00
|
|
|
myself->set_has_pgid_children ();
|
|
|
|
|
2005-06-01 11:46:56 +08:00
|
|
|
if (grouped.ch.parent == NULL)
|
|
|
|
return -1;
|
|
|
|
if (grouped.ch.subproc_ready == NULL)
|
2004-12-06 03:41:26 +08:00
|
|
|
{
|
|
|
|
system_printf ("unable to allocate subproc_ready event, %E");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2007-02-22 20:34:55 +08:00
|
|
|
{
|
|
|
|
hold_everything held_everything (ischild);
|
2009-07-20 22:16:06 +08:00
|
|
|
/* This tmp_pathbuf constructor is required here because the below setjmp
|
|
|
|
magic will otherwise not restore the original buffer count values in
|
|
|
|
the thread-local storage. A process forking too deeply will run into
|
|
|
|
the problem to be out of temporary TLS path buffers. */
|
|
|
|
tmp_pathbuf tp;
|
2011-05-29 04:41:51 +08:00
|
|
|
|
2007-02-22 20:34:55 +08:00
|
|
|
if (!held_everything)
|
|
|
|
{
|
|
|
|
if (exit_state)
|
|
|
|
Sleep (INFINITE);
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-18 00:36:00 +08:00
|
|
|
|
2011-05-30 14:52:12 +08:00
|
|
|
/* Put the dll list in topological dependency ordering, in
|
|
|
|
hopes that the child will have a better shot at loading dlls
|
|
|
|
properly if it only has to deal with one at a time. */
|
|
|
|
dlls.topsort ();
|
|
|
|
|
2007-02-22 20:34:55 +08:00
|
|
|
ischild = !!setjmp (grouped.ch.jmp);
|
2005-11-18 00:36:00 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
volatile char * volatile stackp;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
__asm__ volatile ("movq %%rsp,%0": "=r" (stackp));
|
|
|
|
#else
|
|
|
|
__asm__ volatile ("movl %%esp,%0": "=r" (stackp));
|
|
|
|
#endif
|
2007-02-22 20:34:55 +08:00
|
|
|
|
|
|
|
if (!ischild)
|
2013-04-23 17:44:36 +08:00
|
|
|
res = grouped.parent (stackp);
|
2007-02-22 20:34:55 +08:00
|
|
|
else
|
2007-03-02 02:43:38 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
res = grouped.child (stackp);
|
2011-05-29 04:09:04 +08:00
|
|
|
in_forkee = false;
|
2007-03-02 02:43:38 +08:00
|
|
|
ischild = true; /* might have been reset by fork mem copy */
|
|
|
|
}
|
2007-02-22 20:34:55 +08:00
|
|
|
}
|
2000-10-13 06:15:47 +08:00
|
|
|
|
2010-09-02 02:24:11 +08:00
|
|
|
if (ischild)
|
2010-09-02 05:06:36 +08:00
|
|
|
{
|
|
|
|
myself->process_state |= PID_ACTIVE;
|
2011-05-05 23:03:54 +08:00
|
|
|
myself->process_state &= ~(PID_INITIALIZING | PID_EXITED | PID_REAPED);
|
2010-09-02 05:06:36 +08:00
|
|
|
}
|
2010-09-02 02:24:11 +08:00
|
|
|
else if (res < 0)
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
2011-05-29 04:09:04 +08:00
|
|
|
if (!grouped.errmsg)
|
2005-09-30 08:18:30 +08:00
|
|
|
syscall_printf ("fork failed - child pid %d, errno %d", grouped.child_pid, grouped.this_errno);
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
else if (grouped.ch.silentfail ())
|
|
|
|
debug_printf ("child %d - %s, errno %d", grouped.child_pid,
|
|
|
|
grouped.errmsg, grouped.this_errno);
|
2005-09-28 23:18:49 +08:00
|
|
|
else
|
2017-10-10 00:58:24 +08:00
|
|
|
system_printf ("child %d - %s, errno %d", grouped.child_pid,
|
|
|
|
grouped.errmsg, grouped.this_errno);
|
2005-09-30 08:18:30 +08:00
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
set_errno (grouped.this_errno);
|
|
|
|
}
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%R = fork()", res);
|
2000-10-13 06:15:47 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-11-15 14:27:48 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
void
|
|
|
|
fork_init ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /*DEBUGGING*/
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
|
2002-08-18 13:49:26 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
vfork ()
|
|
|
|
{
|
2004-02-02 02:29:12 +08:00
|
|
|
debug_printf ("stub called");
|
2000-02-18 03:38:33 +08:00
|
|
|
return fork ();
|
|
|
|
}
|
2005-06-01 11:46:56 +08:00
|
|
|
|
2005-12-17 12:36:39 +08:00
|
|
|
/* Copy memory from one process to another. */
|
|
|
|
|
|
|
|
bool
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
child_copy (HANDLE hp, bool write, bool silentfail, ...)
|
2005-06-01 11:46:56 +08:00
|
|
|
{
|
2005-12-17 12:36:39 +08:00
|
|
|
va_list args;
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
va_start (args, silentfail);
|
2005-12-17 12:36:39 +08:00
|
|
|
static const char *huh[] = {"read", "write"};
|
|
|
|
|
|
|
|
char *what;
|
|
|
|
while ((what = va_arg (args, char *)))
|
|
|
|
{
|
|
|
|
char *low = va_arg (args, char *);
|
|
|
|
char *high = va_arg (args, char *);
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T todo = high - low;
|
2005-12-17 12:36:39 +08:00
|
|
|
char *here;
|
|
|
|
|
|
|
|
for (here = low; here < high; here += todo)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T done = 0;
|
2005-12-17 12:36:39 +08:00
|
|
|
if (here + todo > high)
|
|
|
|
todo = high - here;
|
2015-12-03 19:59:56 +08:00
|
|
|
BOOL res;
|
2005-12-17 12:36:39 +08:00
|
|
|
if (write)
|
|
|
|
res = WriteProcessMemory (hp, here, here, todo, &done);
|
|
|
|
else
|
|
|
|
res = ReadProcessMemory (hp, here, here, todo, &done);
|
2005-12-30 04:46:34 +08:00
|
|
|
debug_printf ("%s - hp %p low %p, high %p, res %d", what, hp, low, high, res);
|
2005-12-17 12:36:39 +08:00
|
|
|
if (!res || todo != done)
|
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
__seterrno ();
|
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.
2016-12-07 18:58:28 +08:00
|
|
|
if (silentfail)
|
|
|
|
debug_printf ("%s %s copy failed, %p..%p, done %lu, windows pid %u, %E",
|
|
|
|
what, huh[write], low, high, done, myself->dwProcessId);
|
|
|
|
else
|
|
|
|
/* If this happens then there is a bug in our fork
|
|
|
|
implementation somewhere. */
|
|
|
|
system_printf ("%s %s copy failed, %p..%p, done %lu, windows pid %u, %E",
|
|
|
|
what, huh[write], low, high, done, myself->dwProcessId);
|
2005-12-17 12:36:39 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (args);
|
2005-12-17 12:36:39 +08:00
|
|
|
debug_printf ("done");
|
|
|
|
return true;
|
|
|
|
|
|
|
|
err:
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (args);
|
2005-12-17 12:36:39 +08:00
|
|
|
TerminateProcess (hp, 1);
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return false;
|
2005-06-01 11:46:56 +08:00
|
|
|
}
|