2000-02-17 20:38:33 +01:00
|
|
|
/* fork.cc
|
|
|
|
|
2005-02-20 05:25:33 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005
|
|
|
|
Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
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-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2005-01-12 23:40:46 +01:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-10-01 06:10:07 +02: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 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "dtable.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
#include "cygheap.h"
|
2000-09-01 22:54:22 +02:00
|
|
|
#include "child_info.h"
|
2004-11-26 05:15:10 +01:00
|
|
|
#include "cygtls.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "dll_init.h"
|
2002-08-16 21:41:39 +02:00
|
|
|
#include "sync.h"
|
2002-10-14 22:25:52 +02:00
|
|
|
#include "shared_info.h"
|
2002-08-16 21:41:39 +02:00
|
|
|
#include "cygmalloc.h"
|
2002-10-19 02:57:06 +02:00
|
|
|
#include "cygthread.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-10-28 19:34:29 +02:00
|
|
|
#define NPIDS_HELD 4
|
2004-09-15 01:44:50 +02:00
|
|
|
|
2000-02-17 20:38:33 +01: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 */
|
|
|
|
|
|
|
|
#define dll_data_start &_data_start__
|
|
|
|
#define dll_data_end &_data_end__
|
|
|
|
#define dll_bss_start &_bss_start__
|
|
|
|
#define dll_bss_end &_bss_end__
|
|
|
|
|
|
|
|
static void
|
|
|
|
stack_base (child_info_fork &ch)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
memset (&m, 0, sizeof m);
|
|
|
|
if (!VirtualQuery ((LPCVOID) &m, &m, sizeof m))
|
|
|
|
system_printf ("couldn't get memory info, %E");
|
|
|
|
|
|
|
|
ch.stacktop = m.AllocationBase;
|
|
|
|
ch.stackbottom = (LPBYTE) m.BaseAddress + m.RegionSize;
|
|
|
|
ch.stacksize = (DWORD) ch.stackbottom - (DWORD) &m;
|
|
|
|
debug_printf ("bottom %p, top %p, stack %p, size %d, reserve %d",
|
|
|
|
ch.stackbottom, ch.stacktop, &m, ch.stacksize,
|
|
|
|
(DWORD) ch.stackbottom - (DWORD) ch.stacktop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy memory from parent to child.
|
|
|
|
The result is a boolean indicating success. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
fork_copy (PROCESS_INFORMATION &pi, const char *what, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *low;
|
|
|
|
int pass = 0;
|
|
|
|
|
|
|
|
va_start (args, what);
|
|
|
|
|
|
|
|
while ((low = va_arg (args, char *)))
|
|
|
|
{
|
|
|
|
char *high = va_arg (args, char *);
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-12 19:46:37 +02:00
|
|
|
DWORD todo = wincap.chunksize () ?: high - low;
|
2000-02-17 20:38:33 +01:00
|
|
|
char *here;
|
|
|
|
|
|
|
|
for (here = low; here < high; here += todo)
|
|
|
|
{
|
|
|
|
DWORD done = 0;
|
|
|
|
if (here + todo > high)
|
|
|
|
todo = high - here;
|
|
|
|
int res = WriteProcessMemory (pi.hProcess, here, here, todo, &done);
|
|
|
|
debug_printf ("child handle %p, low %p, high %p, res %d", pi.hProcess,
|
|
|
|
low, high, res);
|
|
|
|
if (!res || todo != done)
|
|
|
|
{
|
|
|
|
if (!res)
|
|
|
|
__seterrno ();
|
|
|
|
/* If this happens then there is a bug in our fork
|
|
|
|
implementation somewhere. */
|
2001-06-18 23:18:59 +02:00
|
|
|
system_printf ("%s pass %d failed, %p..%p, done %d, windows pid %u, %E",
|
|
|
|
what, pass, low, high, done, pi.dwProcessId);
|
2000-02-17 20:38:33 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pass++;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf ("done");
|
|
|
|
return 1;
|
|
|
|
|
2001-08-04 23:10:52 +02:00
|
|
|
err:
|
2000-02-17 20:38:33 +01:00
|
|
|
TerminateProcess (pi.hProcess, 1);
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-12-05 20:41:26 +01:00
|
|
|
resume_child (HANDLE forker_finished)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
SetEvent (forker_finished);
|
2000-10-13 00:15:47 +02:00
|
|
|
debug_printf ("signalled child");
|
|
|
|
return 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2005-04-14 19:08:02 +02:00
|
|
|
/* Notify parent that it is time for the next step. */
|
2000-10-13 00:15:47 +02:00
|
|
|
static void __stdcall
|
2002-09-22 05:38:57 +02:00
|
|
|
sync_with_parent (const char *s, bool hang_self)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-13 00:15:47 +02:00
|
|
|
debug_printf ("signalling parent: %s", s);
|
2004-12-05 20:41:26 +01:00
|
|
|
fork_info->ready (false);
|
2000-10-13 00:15:47 +02:00
|
|
|
if (hang_self)
|
|
|
|
{
|
2002-06-15 23:59:32 +02:00
|
|
|
HANDLE h = fork_info->forker_finished;
|
2000-10-13 00:15:47 +02: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 04:01:58 +01:00
|
|
|
api_fatal ("WFSO timed out %s", s);
|
2000-10-13 00:15:47 +02:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
|
|
|
if (GetLastError () == ERROR_INVALID_HANDLE &&
|
2002-06-15 23:59:32 +02:00
|
|
|
WaitForSingleObject (fork_info->forker_finished, 1) != WAIT_FAILED)
|
2000-10-13 00:15:47 +02:00
|
|
|
break;
|
2004-02-12 04:01:58 +01:00
|
|
|
api_fatal ("WFSO failed %s, fork_finished %p, %E", s,
|
2002-06-15 23:59:32 +02:00
|
|
|
fork_info->forker_finished);
|
2000-10-13 00:15:47 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_printf ("no problems");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
static int __stdcall
|
|
|
|
fork_child (HANDLE& hParent, dll *&first_dll, bool& load_dlls)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-03-22 20:00:31 +01:00
|
|
|
extern void fixup_hooks_after_fork ();
|
2004-01-19 06:46:54 +01:00
|
|
|
extern void fixup_timers_after_fork ();
|
2005-04-01 18:52:48 +02:00
|
|
|
extern void __stdcall create_signal_arrived ();
|
2000-10-13 00:15:47 +02:00
|
|
|
debug_printf ("child is running. pid %d, ppid %d, stack here %p",
|
|
|
|
myself->pid, myself->ppid, __builtin_frame_address (0));
|
|
|
|
|
2003-12-07 23:37:12 +01:00
|
|
|
sync_with_parent ("after longjmp", true);
|
2002-09-30 06:35:18 +02:00
|
|
|
sigproc_printf ("hParent %p, child 1 first_dll %p, load_dlls %d", hParent,
|
2001-09-07 23:32:07 +02:00
|
|
|
first_dll, load_dlls);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
/* If we've played with the stack, stacksize != 0. That means that
|
|
|
|
fork() was invoked from other than the main thread. Make sure that
|
|
|
|
the threadinfo information is properly set up. */
|
2004-02-18 23:32:15 +01:00
|
|
|
if (fork_info->stacksize)
|
2004-02-12 04:01:58 +01:00
|
|
|
{
|
|
|
|
_main_tls = &_my_tls;
|
|
|
|
_main_tls->init_thread (NULL, NULL);
|
|
|
|
_main_tls->local_clib = *_impure_ptr;
|
|
|
|
_impure_ptr = &_main_tls->local_clib;
|
|
|
|
}
|
|
|
|
|
2005-04-03 15:06:43 +02:00
|
|
|
if (wincap.has_security ())
|
|
|
|
{
|
|
|
|
set_cygwin_privileges (hProcImpToken);
|
|
|
|
cygheap->user.reimpersonate ();
|
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
char c;
|
|
|
|
if (GetEnvironmentVariable ("FORKDEBUG", &c, 1))
|
|
|
|
try_to_debug ();
|
|
|
|
char buf[80];
|
|
|
|
/* This is useful for debugging fork problems. Use gdb to attach to
|
|
|
|
the pid reported here. */
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_FORK_SLEEP", buf, sizeof (buf)))
|
|
|
|
{
|
|
|
|
small_printf ("Sleeping %d after fork, pid %u\n", atoi (buf), GetCurrentProcessId ());
|
2002-09-22 05:38:57 +02:00
|
|
|
Sleep (atoi (buf));
|
2000-10-13 00:15:47 +02:00
|
|
|
}
|
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-06-12 13:31:05 +02:00
|
|
|
set_file_api_mode (current_codepage);
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
MALLOC_CHECK;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-19 01:52:59 +02:00
|
|
|
if (fixup_mmaps_after_fork (hParent))
|
|
|
|
api_fatal ("recreate_mmaps_after_fork_failed");
|
|
|
|
|
2005-04-01 17:27:52 +02:00
|
|
|
create_signal_arrived ();
|
|
|
|
|
2005-04-01 13:47:00 +02:00
|
|
|
#ifdef USE_SERVER
|
|
|
|
/* 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");
|
|
|
|
#endif
|
2002-10-21 03:00:58 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
/* If we haven't dynamically loaded any dlls, just signal
|
|
|
|
the parent. Otherwise, load all the dlls, tell the parent
|
|
|
|
that we're done, and wait for the parent to fill in the.
|
|
|
|
loaded dlls' data/bss. */
|
|
|
|
if (!load_dlls)
|
2002-10-22 22:16:31 +02:00
|
|
|
{
|
|
|
|
cygheap->fdtab.fixup_after_fork (hParent);
|
|
|
|
ProtectHandleINH (hParent);
|
2003-12-07 23:37:12 +01:00
|
|
|
sync_with_parent ("performed fork fixup", false);
|
2002-10-22 22:16:31 +02:00
|
|
|
}
|
2000-10-13 00:15:47 +02:00
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-13 00:15:47 +02:00
|
|
|
dlls.load_after_fork (hParent, first_dll);
|
2002-10-22 22:16:31 +02:00
|
|
|
cygheap->fdtab.fixup_after_fork (hParent);
|
|
|
|
ProtectHandleINH (hParent);
|
2003-12-07 23:37:12 +01:00
|
|
|
sync_with_parent ("loaded dlls", true);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
ForceCloseHandle (hParent);
|
2002-07-13 22:00:27 +02:00
|
|
|
(void) ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
|
2000-07-15 04:48:11 +02:00
|
|
|
|
2004-02-17 22:17:25 +01:00
|
|
|
_my_tls.fixup_after_fork ();
|
|
|
|
sigproc_init ();
|
|
|
|
|
2002-09-17 11:12:36 +02:00
|
|
|
pthread::atforkchild ();
|
2004-01-19 06:46:54 +01:00
|
|
|
fixup_timers_after_fork ();
|
2005-03-22 20:00:31 +01:00
|
|
|
fixup_hooks_after_fork ();
|
2000-10-17 01:55:58 +02:00
|
|
|
cygbench ("fork-child");
|
2004-02-12 04:01:58 +01:00
|
|
|
cygwin_finished_initializing = true;
|
2000-10-13 00:15:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-04-24 03:57:07 +02:00
|
|
|
#ifndef NO_SLOW_PID_REUSE
|
2000-11-15 07:27:48 +01:00
|
|
|
static void
|
|
|
|
slow_pid_reuse (HANDLE h)
|
|
|
|
{
|
2005-05-19 01:30:02 +02:00
|
|
|
static NO_COPY HANDLE last_fork_procs[NPIDS_HELD];
|
|
|
|
static NO_COPY unsigned nfork_procs;
|
2000-11-15 07:27:48 +01:00
|
|
|
|
2000-11-15 07:46:19 +01:00
|
|
|
if (nfork_procs >= (sizeof (last_fork_procs) / sizeof (last_fork_procs [0])))
|
2000-11-15 07:27:48 +01:00
|
|
|
nfork_procs = 0;
|
2004-12-05 20:41:26 +01:00
|
|
|
/* Keep a list of handles to child processes sitting around to prevent
|
2000-11-15 07:27:48 +01:00
|
|
|
Windows from reusing the same pid n times in a row. Having the same pids
|
|
|
|
close in succesion confuses bash. Keeping a handle open will stop
|
|
|
|
windows from reusing the same pid. */
|
|
|
|
if (last_fork_procs[nfork_procs])
|
2000-11-15 07:46:19 +01:00
|
|
|
ForceCloseHandle1 (last_fork_procs[nfork_procs], fork_stupidity);
|
|
|
|
if (DuplicateHandle (hMainProc, h, hMainProc, &last_fork_procs[nfork_procs],
|
2000-11-15 07:27:48 +01:00
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS))
|
2000-11-15 07:46:19 +01:00
|
|
|
ProtectHandle1 (last_fork_procs[nfork_procs], fork_stupidity);
|
|
|
|
else
|
2000-11-15 07:27:48 +01:00
|
|
|
{
|
|
|
|
last_fork_procs[nfork_procs] = NULL;
|
|
|
|
system_printf ("couldn't create last_fork_proc, %E");
|
|
|
|
}
|
|
|
|
nfork_procs++;
|
|
|
|
}
|
2003-03-22 02:45:18 +01:00
|
|
|
#endif
|
2000-11-15 07:27:48 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
static int __stdcall
|
2001-06-18 23:18:59 +02:00
|
|
|
fork_parent (HANDLE& hParent, dll *&first_dll,
|
|
|
|
bool& load_dlls, void *stack_here, child_info_fork &ch)
|
2000-10-13 00:15:47 +02:00
|
|
|
{
|
2004-12-05 20:41:26 +01:00
|
|
|
HANDLE forker_finished;
|
2000-10-13 00:15:47 +02:00
|
|
|
DWORD rc;
|
|
|
|
PROCESS_INFORMATION pi = {0, NULL, 0, 0};
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-09-17 11:12:36 +02:00
|
|
|
pthread::atforkprepare ();
|
2001-05-20 03:19:20 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
int c_flags = GetPriorityClass (hMainProc) /*|
|
|
|
|
CREATE_NEW_PROCESS_GROUP*/;
|
|
|
|
STARTUPINFO si = {0, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
|
|
|
|
|
|
|
|
/* If we don't have a console, then don't create a console for the
|
|
|
|
child either. */
|
2002-09-19 17:12:48 +02:00
|
|
|
HANDLE console_handle = CreateFile ("CONOUT$", GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE, &sec_none_nih,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
2000-10-13 00:15:47 +02:00
|
|
|
|
2002-09-19 17:12:48 +02:00
|
|
|
if (console_handle != INVALID_HANDLE_VALUE)
|
2000-10-13 00:15:47 +02:00
|
|
|
CloseHandle (console_handle);
|
|
|
|
else
|
|
|
|
c_flags |= DETACHED_PROCESS;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-26 12:13:41 +02: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. */
|
2001-04-18 23:10:15 +02:00
|
|
|
if (cygheap->fdtab.need_fixup_before ())
|
2000-10-26 12:13:41 +02:00
|
|
|
c_flags |= CREATE_SUSPENDED;
|
|
|
|
|
2000-11-15 07:27:48 +01:00
|
|
|
/* Create an inheritable handle to pass to the child process. This will
|
|
|
|
allow the child to duplicate handles from the parent to itself. */
|
2000-10-13 00:15:47 +02:00
|
|
|
hParent = NULL;
|
2004-11-26 05:15:10 +01:00
|
|
|
if (!DuplicateHandle (hMainProc, hMainProc, hMainProc, &hParent, 0, TRUE,
|
2000-10-13 00:15:47 +02:00
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
system_printf ("couldn't create handle to myself for child, %E");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Remember the address of the first loaded dll and decide
|
|
|
|
if we need to load dlls. We do this here so that this
|
|
|
|
information will be available in the parent and, when
|
|
|
|
the stack is copied, in the child. */
|
|
|
|
first_dll = dlls.start.next;
|
|
|
|
load_dlls = dlls.reload_on_fork && dlls.loaded_dlls;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* This will help some of the confusion. */
|
|
|
|
fflush (stdout);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
forker_finished = CreateEvent (&sec_all, FALSE, FALSE, NULL);
|
|
|
|
if (forker_finished == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle (hParent);
|
2001-10-14 17:49:13 +02:00
|
|
|
system_printf ("unable to allocate forker_finished event, %E");
|
2000-10-13 00:15:47 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
ProtectHandleINH (forker_finished);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
ch.forker_finished = forker_finished;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
stack_base (ch);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
si.cb = sizeof (STARTUPINFO);
|
2004-12-05 20:41:26 +01:00
|
|
|
si.lpReserved2 = (LPBYTE) &ch;
|
2002-09-22 05:38:57 +02:00
|
|
|
si.cbReserved2 = sizeof (ch);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Remove impersonation */
|
2003-06-30 15:07:36 +02:00
|
|
|
cygheap->user.deimpersonate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
ch.parent = hParent;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-14 07:52:38 +02:00
|
|
|
syscall_printf ("CreateProcess (%s, %s, 0, 0, 1, %x, 0, 0, %p, %p)",
|
|
|
|
myself->progname, myself->progname, c_flags, &si, &pi);
|
2004-02-12 04:01:58 +01:00
|
|
|
bool locked = __malloc_lock ();
|
2001-09-14 02:49:00 +02:00
|
|
|
void *newheap;
|
2002-10-09 06:08:05 +02:00
|
|
|
newheap = cygheap_setup_for_child (&ch, cygheap->fdtab.need_fixup_before ());
|
2000-10-14 07:52:38 +02:00
|
|
|
rc = CreateProcess (myself->progname, /* image to run */
|
|
|
|
myself->progname, /* what we send in arg0 */
|
2004-02-06 11:37:37 +01:00
|
|
|
&sec_none_nih,
|
|
|
|
&sec_none_nih,
|
2000-10-14 07:52:38 +02:00
|
|
|
TRUE, /* inherit handles from parent */
|
|
|
|
c_flags,
|
|
|
|
NULL, /* environment filled in later */
|
2000-11-15 07:27:48 +01:00
|
|
|
0, /* use current drive/directory */
|
2000-10-14 07:52:38 +02:00
|
|
|
&si,
|
|
|
|
&pi);
|
2000-07-29 18:24:59 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
CloseHandle (hParent);
|
2000-07-29 18:24:59 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
syscall_printf ("CreateProcessA failed, %E");
|
2002-09-22 05:38:57 +02:00
|
|
|
ForceCloseHandle (forker_finished);
|
2000-06-16 21:36:07 +02:00
|
|
|
/* Restore impersonation */
|
2003-06-30 15:07:36 +02:00
|
|
|
cygheap->user.reimpersonate ();
|
2001-09-14 02:49:00 +02:00
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ch, 0);
|
2004-02-12 04:01:58 +01:00
|
|
|
__malloc_unlock ();
|
2000-10-13 00:15:47 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2000-06-16 21:36:07 +02:00
|
|
|
|
2000-10-26 12:13:41 +02:00
|
|
|
/* Fixup the parent datastructure if needed and resume the child's
|
|
|
|
main thread. */
|
2001-09-14 02:49:00 +02:00
|
|
|
if (!cygheap->fdtab.need_fixup_before ())
|
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ch, 0);
|
|
|
|
else
|
2000-10-26 12:13:41 +02:00
|
|
|
{
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab.fixup_before_fork (pi.dwProcessId);
|
2001-09-14 02:49:00 +02:00
|
|
|
cygheap_setup_for_child_cleanup (newheap, &ch, 1);
|
2000-10-26 12:13:41 +02:00
|
|
|
ResumeThread (pi.hThread);
|
|
|
|
}
|
|
|
|
|
2004-12-05 20:41:26 +01:00
|
|
|
int child_pid = cygwin_pid (pi.dwProcessId);
|
|
|
|
pinfo child (child_pid, 1);
|
|
|
|
child->start_time = time (NULL); /* Register child's starting time. */
|
2005-01-25 23:45:11 +01:00
|
|
|
child->nice = myself->nice;
|
2004-08-31 05:34:04 +02:00
|
|
|
|
2004-12-05 20:41:26 +01:00
|
|
|
if (!child)
|
2003-09-26 05:20:30 +02:00
|
|
|
{
|
|
|
|
syscall_printf ("pinfo failed");
|
|
|
|
if (get_errno () != ENOMEM)
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Initialize things that are done later in dll_crt0_1 that aren't done
|
|
|
|
for the forkee. */
|
2004-12-05 20:41:26 +01:00
|
|
|
strcpy (child->progname, myself->progname);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Restore impersonation */
|
2003-06-30 15:07:36 +02:00
|
|
|
cygheap->user.reimpersonate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
ProtectHandle (pi.hThread);
|
|
|
|
/* Protect the handle but name it similarly to the way it will
|
|
|
|
be called in subproc handling. */
|
2004-12-06 01:29:41 +01:00
|
|
|
ProtectHandle1 (pi.hProcess, childhProc);
|
2000-10-21 06:53:49 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Fill in fields in the child's process table entry. */
|
2004-12-05 20:41:26 +01:00
|
|
|
child->dwProcessId = pi.dwProcessId;
|
|
|
|
child.hProcess = pi.hProcess;
|
2001-02-10 05:20:52 +01: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. */
|
2004-12-23 15:57:08 +01:00
|
|
|
if (!child.remember (false))
|
2001-02-10 05:20:52 +01:00
|
|
|
{
|
|
|
|
TerminateProcess (pi.hProcess, 1);
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2003-04-24 03:57:07 +02:00
|
|
|
#ifndef NO_SLOW_PID_REUSE
|
2001-02-10 05:20:52 +01:00
|
|
|
slow_pid_reuse (pi.hProcess);
|
2003-03-22 02:45:18 +01:00
|
|
|
#endif
|
2000-10-13 00:15:47 +02:00
|
|
|
|
|
|
|
/* Wait for subproc to initialize itself. */
|
2004-12-05 20:41:26 +01:00
|
|
|
if (!ch.sync (child, FORK_WAIT_TIMEOUT))
|
|
|
|
{
|
|
|
|
system_printf ("child %d died waiting for longjmp before initialization", child_pid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-10-13 00:15:47 +02:00
|
|
|
|
|
|
|
/* CHILD IS STOPPED */
|
|
|
|
debug_printf ("child is alive (but stopped)");
|
|
|
|
|
|
|
|
/* Initialize, in order: data, bss, heap, stack, dll data, dll bss
|
|
|
|
Note: variables marked as NO_COPY will not be copied
|
|
|
|
since they are placed in a protected segment. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
MALLOC_CHECK;
|
2003-12-23 17:26:31 +01:00
|
|
|
void *impure_beg;
|
|
|
|
void *impure_end;
|
|
|
|
if (&_my_tls == _main_tls)
|
|
|
|
impure_beg = impure_end = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
impure_beg = _impure_ptr;
|
|
|
|
impure_end = _impure_ptr + 1;
|
|
|
|
}
|
2000-10-13 00:15:47 +02:00
|
|
|
rc = fork_copy (pi, "user/cygwin data",
|
|
|
|
user_data->data_start, user_data->data_end,
|
|
|
|
user_data->bss_start, user_data->bss_end,
|
2002-10-22 18:18:55 +02:00
|
|
|
cygheap->user_heap.base, cygheap->user_heap.ptr,
|
2000-10-13 00:15:47 +02:00
|
|
|
stack_here, ch.stackbottom,
|
|
|
|
dll_data_start, dll_data_end,
|
2003-12-23 17:26:31 +01:00
|
|
|
dll_bss_start, dll_bss_end, impure_beg, impure_end, NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-16 21:41:39 +02:00
|
|
|
__malloc_unlock ();
|
2004-02-12 04:01:58 +01:00
|
|
|
locked = false;
|
2000-10-13 00:15:47 +02:00
|
|
|
MALLOC_CHECK;
|
|
|
|
if (!rc)
|
|
|
|
goto cleanup;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02: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");
|
|
|
|
if (!fork_copy (pi, "linked dll data/bss", d->p.data_start, d->p.data_end,
|
|
|
|
d->p.bss_start, d->p.bss_end,
|
|
|
|
NULL))
|
2000-02-17 20:38:33 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Start thread, and wait for it to reload dlls. */
|
2004-12-05 20:41:26 +01:00
|
|
|
if (!resume_child (forker_finished))
|
2000-10-13 00:15:47 +02:00
|
|
|
goto cleanup;
|
2004-12-05 20:41:26 +01:00
|
|
|
else if (!ch.sync (child, FORK_WAIT_TIMEOUT))
|
|
|
|
{
|
|
|
|
system_printf ("child %d died waiting for dll loading", child_pid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-13 00:15:47 +02: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-17 20:38:33 +01:00
|
|
|
{
|
2000-10-13 00:15:47 +02:00
|
|
|
debug_printf ("copying data/bss for a loaded dll");
|
|
|
|
if (!fork_copy (pi, "loaded dll data/bss", d->p.data_start, d->p.data_end,
|
|
|
|
d->p.bss_start, d->p.bss_end,
|
|
|
|
NULL))
|
|
|
|
goto cleanup;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-10-13 00:15:47 +02:00
|
|
|
/* Start the child up again. */
|
2004-12-05 20:41:26 +01:00
|
|
|
(void) resume_child (forker_finished);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
ForceCloseHandle (pi.hThread);
|
|
|
|
ForceCloseHandle (forker_finished);
|
|
|
|
forker_finished = NULL;
|
|
|
|
pi.hThread = NULL;
|
2002-09-17 11:12:36 +02:00
|
|
|
pthread::atforkparent ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-12-05 20:41:26 +01:00
|
|
|
return child_pid;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Common cleanup code for failure cases */
|
2001-08-04 23:10:52 +02:00
|
|
|
cleanup:
|
2004-02-12 04:01:58 +01:00
|
|
|
if (locked)
|
|
|
|
__malloc_unlock ();
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Remember to de-allocate the fd table. */
|
|
|
|
if (pi.hProcess)
|
|
|
|
ForceCloseHandle1 (pi.hProcess, childhProc);
|
|
|
|
if (pi.hThread)
|
|
|
|
ForceCloseHandle (pi.hThread);
|
|
|
|
if (forker_finished)
|
|
|
|
ForceCloseHandle (forker_finished);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
extern "C" int
|
|
|
|
fork ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-13 00:15:47 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
HANDLE hParent;
|
|
|
|
dll *first_dll;
|
|
|
|
bool load_dlls;
|
|
|
|
} grouped;
|
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
debug_printf ("entering");
|
2000-10-14 07:52:38 +02:00
|
|
|
grouped.hParent = grouped.first_dll = NULL;
|
|
|
|
grouped.load_dlls = 0;
|
2000-10-13 00:15:47 +02:00
|
|
|
|
2000-10-14 07:52:38 +02:00
|
|
|
void *esp;
|
2001-06-18 23:18:59 +02:00
|
|
|
__asm__ volatile ("movl %%esp,%0": "=r" (esp));
|
2000-10-14 07:52:38 +02:00
|
|
|
|
2000-10-25 05:54:50 +02:00
|
|
|
myself->set_has_pgid_children ();
|
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
child_info_fork ch;
|
2004-12-05 20:41:26 +01:00
|
|
|
if (ch.subproc_ready == NULL)
|
|
|
|
{
|
|
|
|
system_printf ("unable to allocate subproc_ready event, %E");
|
|
|
|
return -1;
|
|
|
|
}
|
2000-10-13 00:15:47 +02:00
|
|
|
|
2004-09-12 05:47:57 +02:00
|
|
|
sig_send (NULL, __SIGHOLD);
|
2000-10-14 07:52:38 +02:00
|
|
|
int res = setjmp (ch.jmp);
|
|
|
|
if (res)
|
2000-10-13 00:15:47 +02:00
|
|
|
res = fork_child (grouped.hParent, grouped.first_dll, grouped.load_dlls);
|
|
|
|
else
|
2001-06-18 23:18:59 +02:00
|
|
|
res = fork_parent (grouped.hParent, grouped.first_dll, grouped.load_dlls, esp, ch);
|
2004-09-12 05:47:57 +02:00
|
|
|
sig_send (NULL, __SIGNOHOLD);
|
2000-10-13 00:15:47 +02:00
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
syscall_printf ("%d = fork()", res);
|
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-11-15 07:27:48 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
void
|
|
|
|
fork_init ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /*DEBUGGING*/
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#ifdef NEWVFORK
|
|
|
|
/* Dummy function to force second assignment below to actually be
|
|
|
|
carried out */
|
|
|
|
static vfork_save *
|
|
|
|
get_vfork_val ()
|
|
|
|
{
|
|
|
|
return vfork_storage.val ();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-18 07:49:26 +02:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
vfork ()
|
|
|
|
{
|
|
|
|
#ifndef NEWVFORK
|
2004-02-01 19:29:12 +01:00
|
|
|
debug_printf ("stub called");
|
2000-02-17 20:38:33 +01:00
|
|
|
return fork ();
|
|
|
|
#else
|
|
|
|
vfork_save *vf = get_vfork_val ();
|
2000-08-03 05:02:41 +02:00
|
|
|
char **esp, **pp;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (vf == NULL)
|
|
|
|
vf = vfork_storage.create ();
|
2001-09-14 06:37:57 +02:00
|
|
|
else if (vf->pid)
|
|
|
|
return fork ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-11-28 21:55:59 +01:00
|
|
|
// FIXME the tls stuff could introduce a signal race if a child process
|
|
|
|
// exits quickly.
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!setjmp (vf->j))
|
|
|
|
{
|
|
|
|
vf->pid = -1;
|
2000-08-03 05:02:41 +02:00
|
|
|
__asm__ volatile ("movl %%esp,%0": "=r" (vf->vfork_esp):);
|
2000-02-17 20:38:33 +01:00
|
|
|
__asm__ volatile ("movl %%ebp,%0": "=r" (vf->vfork_ebp):);
|
2003-11-28 21:55:59 +01:00
|
|
|
for (pp = (char **) vf->frame, esp = vf->vfork_esp;
|
2001-09-15 02:47:44 +02:00
|
|
|
esp <= vf->vfork_ebp + 2; pp++, esp++)
|
2000-08-03 05:02:41 +02:00
|
|
|
*pp = *esp;
|
2002-08-18 07:49:26 +02:00
|
|
|
vf->ctty = myself->ctty;
|
|
|
|
vf->sid = myself->sid;
|
|
|
|
vf->pgid = myself->pgid;
|
2004-01-03 19:15:03 +01:00
|
|
|
cygheap->ctty_on_hold = cygheap->ctty;
|
2003-12-30 02:57:16 +01:00
|
|
|
vf->open_fhs = cygheap->open_fhs;
|
2004-01-14 16:45:37 +01:00
|
|
|
debug_printf ("cygheap->ctty_on_hold %p, cygheap->open_fhs %d", cygheap->ctty_on_hold, cygheap->open_fhs);
|
2001-06-24 23:57:50 +02:00
|
|
|
int res = cygheap->fdtab.vfork_child_dup () ? 0 : -1;
|
|
|
|
debug_printf ("%d = vfork()", res);
|
2004-02-12 04:01:58 +01:00
|
|
|
_my_tls.call_signal_handler (); // FIXME: racy
|
2003-11-28 21:55:59 +01:00
|
|
|
vf->tls = _my_tls;
|
2001-06-24 23:57:50 +02:00
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vf = get_vfork_val ();
|
2001-09-14 02:49:00 +02:00
|
|
|
|
2003-11-28 21:55:59 +01:00
|
|
|
for (pp = (char **) vf->frame, esp = vf->vfork_esp;
|
2001-09-15 02:47:44 +02:00
|
|
|
esp <= vf->vfork_ebp + 2; pp++, esp++)
|
2001-09-14 02:49:00 +02:00
|
|
|
*esp = *pp;
|
|
|
|
|
2001-09-15 02:47:44 +02:00
|
|
|
cygheap->fdtab.vfork_parent_restore ();
|
|
|
|
|
2002-08-18 07:49:26 +02:00
|
|
|
myself->ctty = vf->ctty;
|
|
|
|
myself->sid = vf->sid;
|
|
|
|
myself->pgid = vf->pgid;
|
2004-01-03 19:15:03 +01:00
|
|
|
termios_printf ("cygheap->ctty %p, cygheap->ctty_on_hold %p", cygheap->ctty, cygheap->ctty_on_hold);
|
|
|
|
cygheap->open_fhs = vf->open_fhs;
|
2002-08-18 07:49:26 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
if (vf->pid < 0)
|
|
|
|
{
|
2002-08-18 07:49:26 +02:00
|
|
|
int exitval = vf->exitval;
|
2001-09-14 06:39:56 +02:00
|
|
|
vf->pid = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
if ((vf->pid = fork ()) == 0)
|
|
|
|
exit (exitval);
|
|
|
|
}
|
|
|
|
|
2001-09-14 06:37:57 +02:00
|
|
|
int pid = vf->pid;
|
|
|
|
vf->pid = 0;
|
2001-10-04 04:34:20 +02:00
|
|
|
debug_printf ("exiting vfork, pid %d", pid);
|
2001-09-15 02:47:44 +02:00
|
|
|
sig_dispatch_pending ();
|
2003-11-28 21:55:59 +01:00
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
_my_tls.call_signal_handler (); // FIXME: racy
|
2003-11-28 21:55:59 +01:00
|
|
|
_my_tls = vf->tls;
|
2001-09-14 06:37:57 +02:00
|
|
|
return pid;
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
|
|
|
}
|