2000-02-18 03:38:33 +08:00
|
|
|
/* dcrt0.cc -- essentially the main() for the Cygwin dll
|
|
|
|
|
2009-01-03 13:12:22 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
2015-07-29 19:45:46 +08:00
|
|
|
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2008-04-08 00:15:45 +08:00
|
|
|
#include "miscfuncs.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "glob.h"
|
|
|
|
#include <ctype.h>
|
2009-03-24 20:18:34 +08:00
|
|
|
#include <locale.h>
|
2015-07-05 23:45:06 +08:00
|
|
|
#include <sys/param.h>
|
2009-01-03 13:12:22 +08:00
|
|
|
#include "environ.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sigproc.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#define NEED_VFORK
|
2001-03-05 14:28:25 +08:00
|
|
|
#include "perprocess.h"
|
2001-06-04 09:28:09 +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"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "dtable.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "cygheap.h"
|
2001-12-26 12:53:34 +08:00
|
|
|
#include "child_info_magic.h"
|
2004-11-26 12:15:10 +08:00
|
|
|
#include "cygtls.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include "shared_info.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "cygwin_version.h"
|
|
|
|
#include "dll_init.h"
|
2004-01-14 23:45:37 +08:00
|
|
|
#include "heap.h"
|
2008-04-02 18:34:17 +08:00
|
|
|
#include "tls_pbuf.h"
|
2010-02-28 23:54:25 +08:00
|
|
|
#include "exception.h"
|
2010-03-03 23:05:19 +08:00
|
|
|
#include "cygxdr.h"
|
2011-03-17 16:40:25 +08:00
|
|
|
#include "fenv.h"
|
2010-08-28 16:51:21 +08:00
|
|
|
#include "ntdll.h"
|
2011-12-16 19:58:03 +08:00
|
|
|
#include "wow64.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#define MAX_AT_FILE_LEVEL 10
|
|
|
|
|
2001-03-03 11:56:34 +08:00
|
|
|
#define PREMAIN_LEN (sizeof (user_data->premain) / sizeof (user_data->premain[0]))
|
2000-07-01 11:51:55 +08:00
|
|
|
|
2009-01-03 13:12:22 +08:00
|
|
|
extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
|
2003-12-12 13:04:47 +08:00
|
|
|
extern "C" void __sinit (_reent *);
|
|
|
|
|
2009-01-03 13:12:22 +08:00
|
|
|
static int NO_COPY envc;
|
|
|
|
static char NO_COPY **envp;
|
2002-06-28 04:44:27 +08:00
|
|
|
|
2009-07-04 02:05:51 +08:00
|
|
|
bool NO_COPY jit_debug;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
2005-05-12 05:53:54 +08:00
|
|
|
do_global_dtors ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-04-15 01:34:03 +08:00
|
|
|
void (**pfunc) () = user_data->dtors;
|
|
|
|
if (pfunc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-04-15 01:34:03 +08:00
|
|
|
user_data->dtors = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
while (*++pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
do_global_ctors (void (**in_pfunc)(), int force)
|
|
|
|
{
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!force && in_forkee)
|
2003-12-24 00:26:31 +08:00
|
|
|
return; // inherit constructed stuff from parent pid
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Run ctors backwards, so skip the first entry and find how many
|
|
|
|
there are, then run them. */
|
|
|
|
|
2003-12-24 00:26:31 +08:00
|
|
|
void (**pfunc) () = in_pfunc;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
while (*++pfunc)
|
|
|
|
;
|
|
|
|
while (--pfunc > in_pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-08-15 23:46:27 +08:00
|
|
|
* Replaces @file in the command line with the contents of the file.
|
|
|
|
* There may be multiple @file's in a single command line
|
|
|
|
* A \@file is replaced with @file so that echo \@foo would print
|
|
|
|
* @foo and not the contents of foo.
|
2000-02-18 03:38:33 +08:00
|
|
|
*/
|
2003-12-08 06:37:12 +08:00
|
|
|
static bool __stdcall
|
2000-05-07 11:29:08 +08:00
|
|
|
insert_file (char *name, char *&cmd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
HANDLE f;
|
|
|
|
DWORD size;
|
2008-04-02 18:34:17 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
|
|
|
|
PWCHAR wname = tp.w_get ();
|
2008-04-04 00:00:14 +08:00
|
|
|
sys_mbstowcs (wname, NT_MAX_PATH, name + 1);
|
2008-04-02 18:34:17 +08:00
|
|
|
f = CreateFileW (wname,
|
2011-07-04 23:25:36 +08:00
|
|
|
GENERIC_READ, /* open for reading */
|
|
|
|
FILE_SHARE_VALID_FLAGS, /* share for reading */
|
|
|
|
&sec_none_nih, /* default security */
|
|
|
|
OPEN_EXISTING, /* existing file only */
|
|
|
|
FILE_ATTRIBUTE_NORMAL, /* normal file */
|
|
|
|
NULL); /* no attr. template */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (f == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
debug_printf ("couldn't open file '%s', %E", name);
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This only supports files up to about 4 billion bytes in
|
|
|
|
size. I am making the bold assumption that this is big
|
|
|
|
enough for this feature */
|
|
|
|
size = GetFileSize (f, NULL);
|
|
|
|
if (size == 0xFFFFFFFF)
|
|
|
|
{
|
2014-06-23 18:18:26 +08:00
|
|
|
CloseHandle (f);
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("couldn't get file size for '%s', %E", name);
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int new_size = strlen (cmd) + size + 2;
|
|
|
|
char *tmp = (char *) malloc (new_size);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
2014-06-23 18:18:26 +08:00
|
|
|
CloseHandle (f);
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("malloc failed, %E");
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* realloc passed as it should */
|
|
|
|
DWORD rf_read;
|
|
|
|
BOOL rf_result;
|
|
|
|
rf_result = ReadFile (f, tmp, size, &rf_read, NULL);
|
|
|
|
CloseHandle (f);
|
|
|
|
if (!rf_result || (rf_read != size))
|
|
|
|
{
|
2014-06-23 18:18:26 +08:00
|
|
|
free (tmp);
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("ReadFile failed, %E");
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp[size++] = ' ';
|
|
|
|
strcpy (tmp + size, cmd);
|
|
|
|
cmd = tmp;
|
2003-12-08 06:37:12 +08:00
|
|
|
return true;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
isquote (char c)
|
|
|
|
{
|
|
|
|
char ch = c;
|
|
|
|
return ch == '"' || ch == '\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step over a run of characters delimited by quotes */
|
2000-07-16 14:31:06 +08:00
|
|
|
static /*__inline*/ char *
|
2000-02-21 13:20:38 +08:00
|
|
|
quoted (char *cmd, int winshell)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char quote = *cmd;
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!winshell)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
char *p;
|
|
|
|
strcpy (cmd, cmd + 1);
|
2012-10-27 20:09:38 +08:00
|
|
|
if (*(p = strchrnul (cmd, quote)))
|
2000-09-03 12:16:35 +08:00
|
|
|
strcpy (p, p + 1);
|
2000-09-14 03:57:00 +08:00
|
|
|
return p;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-09-26 08:52:21 +08:00
|
|
|
const char *s = quote == '\'' ? "'" : "\\\"";
|
2000-09-03 12:16:35 +08:00
|
|
|
/* This must have been run from a Windows shell, so preserve
|
|
|
|
quotes for globify to play with later. */
|
2000-09-26 08:52:21 +08:00
|
|
|
while (*cmd && *++cmd)
|
|
|
|
if ((p = strpbrk (cmd, s)) == NULL)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
cmd = strchr (cmd, '\0'); // no closing quote
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (*p == '\\')
|
|
|
|
cmd = ++p;
|
2000-09-26 08:52:21 +08:00
|
|
|
else if (quote == '"' && p[1] == '"')
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
*p = '\\';
|
|
|
|
cmd = ++p; // a quoted quote
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd = p + 1; // point to after end
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cmd;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform a glob on word if it contains wildcard characters.
|
|
|
|
Also quote every character between quotes to force glob to
|
|
|
|
treat the characters literally. */
|
2012-01-27 17:28:38 +08:00
|
|
|
|
|
|
|
/* Either X:[...] or \\server\[...] */
|
|
|
|
#define is_dos_path(s) (isdrive(s) \
|
|
|
|
|| ((s)[0] == '\\' \
|
|
|
|
&& (s)[1] == '\\' \
|
|
|
|
&& isalpha ((s)[2]) \
|
|
|
|
&& strchr ((s) + 3, '\\')))
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static int __stdcall
|
|
|
|
globify (char *word, char **&argv, int &argc, int &argvlen)
|
|
|
|
{
|
|
|
|
if (*word != '~' && strpbrk (word, "?*[\"\'(){}") == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
char *p, *s;
|
2012-01-27 17:28:38 +08:00
|
|
|
int dos_spec = is_dos_path (word);
|
2001-03-03 11:56:34 +08:00
|
|
|
if (!dos_spec && isquote (*word) && word[1] && word[2])
|
2012-01-27 17:28:38 +08:00
|
|
|
dos_spec = is_dos_path (word + 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* We'll need more space if there are quoting characters in
|
|
|
|
word. If that is the case, doubling the size of the
|
|
|
|
string should provide more than enough space. */
|
|
|
|
if (strpbrk (word, "'\""))
|
|
|
|
n = strlen (word);
|
|
|
|
char pattern[strlen (word) + ((dos_spec + 1) * n) + 1];
|
|
|
|
|
|
|
|
/* Fill pattern with characters from word, quoting any
|
|
|
|
characters found within quotes. */
|
|
|
|
for (p = pattern, s = word; *s != '\000'; s++, p++)
|
|
|
|
if (!isquote (*s))
|
|
|
|
{
|
|
|
|
if (dos_spec && *s == '\\')
|
|
|
|
*p++ = '\\';
|
|
|
|
*p = *s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char quote = *s;
|
|
|
|
while (*++s && *s != quote)
|
|
|
|
{
|
2000-04-19 07:16:56 +08:00
|
|
|
if (dos_spec || *s != '\\')
|
2000-04-19 05:28:45 +08:00
|
|
|
/* nothing */;
|
|
|
|
else if (s[1] == quote || s[1] == '\\')
|
2000-02-18 03:38:33 +08:00
|
|
|
s++;
|
|
|
|
*p++ = '\\';
|
2009-04-18 19:08:37 +08:00
|
|
|
size_t cnt = isascii (*s) ? 1 : mbtowc (NULL, s, MB_CUR_MAX);
|
2009-04-17 22:51:44 +08:00
|
|
|
if (cnt <= 1 || cnt == (size_t)-1)
|
|
|
|
*p++ = *s;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--s;
|
2011-06-06 13:02:13 +08:00
|
|
|
while (cnt-- > 0)
|
2009-04-17 22:51:44 +08:00
|
|
|
*p++ = *++s;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
if (*s == quote)
|
|
|
|
p--;
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
glob_t gl;
|
|
|
|
gl.gl_offs = 0;
|
|
|
|
|
|
|
|
/* Attempt to match the argument. Return just word (minus quoting) if no match. */
|
|
|
|
if (glob (pattern, GLOB_TILDE | GLOB_NOCHECK | GLOB_BRACE | GLOB_QUOTE, NULL, &gl) || !gl.gl_pathc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Allocate enough space in argv for the matched filenames. */
|
|
|
|
n = argc;
|
|
|
|
if ((argc += gl.gl_pathc) > argvlen)
|
|
|
|
{
|
|
|
|
argvlen = argc + 10;
|
|
|
|
argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the matched filenames to argv. */
|
|
|
|
char **gv = gl.gl_pathv;
|
|
|
|
char **av = argv + n;
|
|
|
|
while (*gv)
|
|
|
|
{
|
2002-09-30 12:35:18 +08:00
|
|
|
debug_printf ("argv[%d] = '%s'", n++, *gv);
|
2000-02-18 03:38:33 +08:00
|
|
|
*av++ = *gv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up after glob. */
|
|
|
|
free (gl.gl_pathv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build argv, argc from string passed from Windows. */
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
build_argv (char *cmd, char **&argv, int &argc, int winshell)
|
|
|
|
{
|
|
|
|
int argvlen = 0;
|
|
|
|
int nesting = 0; // monitor "nesting" from insert_file
|
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
argvlen = 0;
|
|
|
|
argv = NULL;
|
|
|
|
|
|
|
|
/* Scan command line until there is nothing left. */
|
|
|
|
while (*cmd)
|
|
|
|
{
|
|
|
|
/* Ignore spaces */
|
|
|
|
if (issep (*cmd))
|
|
|
|
{
|
|
|
|
cmd++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Found the beginning of an argument. */
|
|
|
|
char *word = cmd;
|
|
|
|
char *sawquote = NULL;
|
|
|
|
while (*cmd)
|
|
|
|
{
|
|
|
|
if (*cmd != '"' && (!winshell || *cmd != '\''))
|
|
|
|
cmd++; // Skip over this character
|
|
|
|
else
|
|
|
|
/* Skip over characters until the closing quote */
|
|
|
|
{
|
|
|
|
sawquote = cmd;
|
2012-12-20 21:39:34 +08:00
|
|
|
/* Handle quoting. Only strip off quotes if the parent is
|
|
|
|
a Cygwin process, or if the word starts with a '@'.
|
|
|
|
In this case, the insert_file function needs an unquoted
|
|
|
|
DOS filename and globbing isn't performed anyway. */
|
|
|
|
cmd = quoted (cmd, winshell && argc > 0 && *word != '@');
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
if (issep (*cmd)) // End of argument if space
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*cmd)
|
|
|
|
*cmd++ = '\0'; // Terminate `word'
|
|
|
|
|
|
|
|
/* Possibly look for @file construction assuming that this isn't
|
|
|
|
the very first argument and the @ wasn't quoted */
|
|
|
|
if (argc && sawquote != word && *word == '@')
|
|
|
|
{
|
|
|
|
if (++nesting > MAX_AT_FILE_LEVEL)
|
|
|
|
api_fatal ("Too many levels of nesting for %s", word);
|
2000-05-07 11:29:08 +08:00
|
|
|
if (insert_file (word, cmd))
|
2000-02-18 03:38:33 +08:00
|
|
|
continue; // There's new stuff in cmd now
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we need to allocate more space for argv */
|
|
|
|
if (argc >= argvlen)
|
|
|
|
{
|
|
|
|
argvlen = argc + 10;
|
|
|
|
argv = (char **) realloc (argv, (1 + argvlen) * sizeof (argv[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add word to argv file after (optional) wildcard expansion. */
|
|
|
|
if (!winshell || !argc || !globify (word, argv, argc, argvlen))
|
|
|
|
{
|
2002-09-30 12:35:18 +08:00
|
|
|
debug_printf ("argv[%d] = '%s'", argc, word);
|
2000-02-18 03:38:33 +08:00
|
|
|
argv[argc++] = word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-05 01:30:55 +08:00
|
|
|
if (argv)
|
|
|
|
argv[argc] = NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
|
|
|
debug_printf ("argc %d", argc);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity and sync check */
|
|
|
|
void __stdcall
|
|
|
|
check_sanity_and_sync (per_process *p)
|
|
|
|
{
|
|
|
|
/* Sanity check to make sure developers didn't change the per_process */
|
|
|
|
/* struct without updating SIZEOF_PER_PROCESS [it makes them think twice */
|
|
|
|
/* about changing it]. */
|
|
|
|
if (sizeof (per_process) != SIZEOF_PER_PROCESS)
|
2006-12-12 02:55:29 +08:00
|
|
|
api_fatal ("per_process sanity check failed");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Make sure that the app and the dll are in sync. */
|
|
|
|
|
|
|
|
/* Complain if older than last incompatible change */
|
|
|
|
if (p->dll_major < CYGWIN_VERSION_DLL_EPOCH)
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("cygwin DLL and APP are out of sync -- DLL version mismatch %u < %u",
|
2000-02-18 03:38:33 +08:00
|
|
|
p->dll_major, CYGWIN_VERSION_DLL_EPOCH);
|
|
|
|
|
|
|
|
/* magic_biscuit != 0 if using the old style version numbering scheme. */
|
|
|
|
if (p->magic_biscuit != SIZEOF_PER_PROCESS)
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("Incompatible cygwin .dll -- incompatible per_process info %u != %u",
|
2000-02-18 03:38:33 +08:00
|
|
|
p->magic_biscuit, SIZEOF_PER_PROCESS);
|
|
|
|
|
|
|
|
/* Complain if incompatible API changes made */
|
2003-11-23 01:22:59 +08:00
|
|
|
if (p->api_major > cygwin_version.api_major)
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %u > %u",
|
2000-02-18 03:38:33 +08:00
|
|
|
p->api_major, cygwin_version.api_major);
|
2009-10-02 14:04:57 +08:00
|
|
|
|
2014-10-10 23:29:18 +08:00
|
|
|
#ifndef __x86_64__
|
2009-10-02 14:04:57 +08:00
|
|
|
/* This is a kludge to work around a version of _cygwin_common_crt0
|
|
|
|
which overwrote the cxx_malloc field with the local DLL copy.
|
|
|
|
Hilarity ensues if the DLL is not loaded while the process
|
|
|
|
is forking. */
|
|
|
|
__cygwin_user_data.cxx_malloc = &default_cygwin_cxx_malloc;
|
2014-10-10 23:29:18 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-03-31 20:35:44 +08:00
|
|
|
child_info NO_COPY *child_proc_info;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2015-07-07 23:24:37 +08:00
|
|
|
/* Extend the stack prior to fork longjmp. */
|
2006-04-04 01:33:07 +08:00
|
|
|
void
|
2015-07-07 23:24:37 +08:00
|
|
|
child_info_fork::alloc_stack ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2015-07-07 23:24:37 +08:00
|
|
|
/* Make sure not to try a hard allocation if we have been forked off from
|
|
|
|
the main thread of a Cygwin process which has been started from a 64 bit
|
|
|
|
parent. In that case the _tlsbase of the forked child is not the same
|
|
|
|
as the _tlsbase of the parent (== stackbottom), but only because the
|
|
|
|
stack of the parent has been slightly rearranged. See comment in
|
|
|
|
wow64_revert_to_original_stack for details. We check here if the
|
|
|
|
parent stack fits into the child stack. */
|
|
|
|
if (_tlsbase != stackbottom
|
|
|
|
&& (!wincap.is_wow64 ()
|
|
|
|
|| stacktop < NtCurrentTeb ()->DeallocationStack
|
|
|
|
|| stackbottom > _tlsbase))
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2015-07-07 23:24:37 +08:00
|
|
|
void *stack_ptr;
|
|
|
|
size_t stacksize;
|
|
|
|
|
|
|
|
/* If guardsize is -1, we have been started from a pthread with an
|
|
|
|
application-provided stack, and the stack has just to be used as is. */
|
|
|
|
if (guardsize == (size_t) -1)
|
|
|
|
return;
|
|
|
|
/* Reserve entire stack. */
|
|
|
|
stacksize = (PBYTE) stackbottom - (PBYTE) stackaddr;
|
|
|
|
if (!VirtualAlloc (stackaddr, stacksize, MEM_RESERVE, PAGE_NOACCESS))
|
|
|
|
{
|
|
|
|
PTEB teb = NtCurrentTeb ();
|
|
|
|
api_fatal ("fork: can't reserve memory for parent stack "
|
|
|
|
"%p - %p, (child has %p - %p), %E",
|
|
|
|
stackaddr, stackbottom, teb->DeallocationStack, _tlsbase);
|
|
|
|
}
|
|
|
|
/* Commit the area commited in parent. */
|
|
|
|
stacksize = (PBYTE) stackbottom - (PBYTE) stacktop;
|
|
|
|
stack_ptr = VirtualAlloc (stacktop, stacksize, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE);
|
|
|
|
if (!stack_ptr)
|
|
|
|
api_fatal ("can't commit memory for stack %p(%ly), %E",
|
|
|
|
stacktop, stacksize);
|
|
|
|
/* Set up guardpages. */
|
2015-07-05 23:45:06 +08:00
|
|
|
ULONG real_guardsize = guardsize
|
|
|
|
? roundup2 (guardsize, wincap.page_size ())
|
|
|
|
: wincap.def_guard_page_size ();
|
2011-05-20 15:23:11 +08:00
|
|
|
if (stack_ptr > stackaddr)
|
|
|
|
{
|
2015-07-05 23:45:06 +08:00
|
|
|
stack_ptr = (void *) ((PBYTE) stack_ptr - real_guardsize);
|
|
|
|
if (!VirtualAlloc (stack_ptr, real_guardsize, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE | PAGE_GUARD))
|
2011-05-20 15:23:11 +08:00
|
|
|
api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
|
|
|
|
stack_ptr);
|
|
|
|
}
|
2015-07-07 23:24:37 +08:00
|
|
|
/* On post-XP systems, set thread stack guarantee matching the
|
|
|
|
guardsize. Note that the guardsize is one page bigger than
|
|
|
|
the guarantee. */
|
2015-07-05 23:45:06 +08:00
|
|
|
if (wincap.has_set_thread_stack_guarantee ()
|
|
|
|
&& real_guardsize > wincap.def_guard_page_size ())
|
|
|
|
{
|
|
|
|
real_guardsize -= wincap.page_size ();
|
|
|
|
SetThreadStackGuarantee (&real_guardsize);
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2004-02-14 12:38:37 +08:00
|
|
|
else
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
2015-07-07 23:24:37 +08:00
|
|
|
/* Fork has been called from main thread. Simply commit the region
|
|
|
|
of the stack commited in the parent but not yet commited in the
|
|
|
|
child and create new guardpages. */
|
|
|
|
if (_tlstop > stacktop)
|
|
|
|
{
|
|
|
|
SIZE_T commitsize = (PBYTE) _tlstop - (PBYTE) stacktop;
|
|
|
|
if (!VirtualAlloc (stacktop, commitsize, MEM_COMMIT, PAGE_READWRITE))
|
|
|
|
api_fatal ("can't commit child memory for stack %p(%ly), %E",
|
|
|
|
stacktop, commitsize);
|
|
|
|
PVOID guardpage = (PBYTE) stacktop - wincap.def_guard_page_size ();
|
|
|
|
if (!VirtualAlloc (guardpage, wincap.def_guard_page_size (),
|
|
|
|
MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD))
|
|
|
|
api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
|
|
|
|
guardpage);
|
|
|
|
_tlstop = stacktop;
|
|
|
|
}
|
2011-05-20 15:23:11 +08:00
|
|
|
stackaddr = 0;
|
2011-12-16 19:58:03 +08:00
|
|
|
/* This only affects forked children of a process started from a native
|
2011-12-18 07:39:47 +08:00
|
|
|
64 bit process, but it doesn't hurt to do it unconditionally. Fix
|
2011-12-16 19:58:03 +08:00
|
|
|
StackBase in the child to be the same as in the parent, so that the
|
|
|
|
computation of _my_tls is correct. */
|
2013-04-23 17:44:36 +08:00
|
|
|
_tlsbase = (PVOID) stackbottom;
|
2005-09-28 23:18:49 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2006-06-03 10:58:59 +08:00
|
|
|
extern "C" void
|
2004-01-14 23:45:37 +08:00
|
|
|
break_here ()
|
|
|
|
{
|
2006-06-03 10:58:59 +08:00
|
|
|
static int NO_COPY sent_break;
|
|
|
|
if (!sent_break++)
|
|
|
|
DebugBreak ();
|
2004-01-14 23:45:37 +08:00
|
|
|
debug_printf ("break here");
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
2004-02-19 06:32:15 +08:00
|
|
|
initial_env ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_TESTING", NULL, 0))
|
2004-01-20 07:03:43 +08:00
|
|
|
_cygwin_testing = 1;
|
2004-02-19 06:32:15 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
#ifdef DEBUGGING
|
2015-07-20 04:38:30 +08:00
|
|
|
char buf[PATH_MAX];
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2015-07-20 04:38:30 +08:00
|
|
|
char buf1[PATH_MAX];
|
|
|
|
GetModuleFileName (NULL, buf1, PATH_MAX);
|
2005-03-10 22:41:50 +08:00
|
|
|
char *p = strpbrk (buf, ":=");
|
2004-01-14 23:45:37 +08:00
|
|
|
if (!p)
|
|
|
|
p = (char *) "gdb.exe -nw";
|
|
|
|
else
|
|
|
|
*p++ = '\0';
|
2014-08-19 16:41:40 +08:00
|
|
|
if (strcasestr (buf1, buf))
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2015-07-20 04:38:30 +08:00
|
|
|
extern PWCHAR debugger_command;
|
|
|
|
|
|
|
|
debugger_command = (PWCHAR) HeapAlloc (GetProcessHeap (), 0,
|
|
|
|
(2 * NT_MAX_PATH + 20)
|
|
|
|
* sizeof (WCHAR));
|
|
|
|
if (!debugger_command)
|
|
|
|
return;
|
2004-01-14 23:45:37 +08:00
|
|
|
error_start_init (p);
|
2009-07-04 02:05:51 +08:00
|
|
|
jit_debug = true;
|
2004-01-14 23:45:37 +08:00
|
|
|
try_to_debug ();
|
2004-02-19 06:32:15 +08:00
|
|
|
console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n");
|
2004-01-14 23:45:37 +08:00
|
|
|
break_here ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2005-05-09 09:19:38 +08:00
|
|
|
child_info *
|
|
|
|
get_cygwin_startup_info ()
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2005-05-09 09:19:38 +08:00
|
|
|
STARTUPINFO si;
|
2002-06-11 01:08:09 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
GetStartupInfo (&si);
|
2005-05-09 09:19:38 +08:00
|
|
|
child_info *res = (child_info *) si.lpReserved2;
|
2006-12-05 18:59:21 +08:00
|
|
|
|
2005-05-09 09:19:38 +08:00
|
|
|
if (si.cbReserved2 < EXEC_MAGIC_SIZE || !res
|
2006-12-12 02:55:29 +08:00
|
|
|
|| res->intro != PROC_MAGIC_GENERIC || res->magic != CHILD_INFO_MAGIC)
|
2011-11-25 05:36:53 +08:00
|
|
|
{
|
|
|
|
strace.activate (false);
|
|
|
|
res = NULL;
|
|
|
|
}
|
2004-01-14 23:45:37 +08:00
|
|
|
else
|
|
|
|
{
|
2005-05-09 09:19:38 +08:00
|
|
|
if ((res->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
|
|
|
|
multiple_cygwin_problem ("proc intro", res->intro, 0);
|
|
|
|
else if (res->cygheap != (void *) &_cygheap_start)
|
2013-04-23 17:44:36 +08:00
|
|
|
multiple_cygwin_problem ("cygheap base", (uintptr_t) res->cygheap,
|
|
|
|
(uintptr_t) &_cygheap_start);
|
2005-05-09 09:19:38 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
unsigned should_be_cb = 0;
|
2005-05-09 09:19:38 +08:00
|
|
|
switch (res->type)
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
case _CH_FORK:
|
2005-12-30 04:46:34 +08:00
|
|
|
in_forkee = true;
|
2004-01-14 23:45:37 +08:00
|
|
|
should_be_cb = sizeof (child_info_fork);
|
|
|
|
/* fall through */;
|
2011-11-14 09:29:49 +08:00
|
|
|
case _CH_SPAWN:
|
|
|
|
case _CH_EXEC:
|
2004-01-14 23:45:37 +08:00
|
|
|
if (!should_be_cb)
|
2004-12-06 03:41:26 +08:00
|
|
|
should_be_cb = sizeof (child_info_spawn);
|
2005-05-09 09:19:38 +08:00
|
|
|
if (should_be_cb != res->cb)
|
|
|
|
multiple_cygwin_problem ("proc size", res->cb, should_be_cb);
|
|
|
|
else if (sizeof (fhandler_union) != res->fhandler_union_cb)
|
2013-04-23 17:44:36 +08:00
|
|
|
multiple_cygwin_problem ("fhandler size", res->fhandler_union_cb,
|
|
|
|
sizeof (fhandler_union));
|
2006-03-21 02:01:17 +08:00
|
|
|
if (res->isstraced ())
|
2005-12-30 04:46:34 +08:00
|
|
|
{
|
2011-11-25 05:36:53 +08:00
|
|
|
while (!being_debugged ())
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2011-11-24 10:17:55 +08:00
|
|
|
strace.activate (res->type == _CH_FORK);
|
2005-12-30 04:46:34 +08:00
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
break;
|
2004-01-14 23:45:37 +08:00
|
|
|
default:
|
2013-04-23 17:44:36 +08:00
|
|
|
system_printf ("unknown exec type %u", res->type);
|
2004-01-14 23:45:37 +08:00
|
|
|
/* intentionally fall through */
|
2011-11-14 09:29:49 +08:00
|
|
|
case _CH_WHOOPS:
|
2005-05-09 09:19:38 +08:00
|
|
|
res = NULL;
|
2004-01-14 23:45:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 09:19:38 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#define dll_data_start &__data_start__
|
|
|
|
#define dll_data_end &__data_end__
|
|
|
|
#define dll_bss_start &__bss_start__
|
|
|
|
#define dll_bss_end &__bss_end__
|
|
|
|
#else
|
2005-12-17 12:36:39 +08:00
|
|
|
#define dll_data_start &_data_start__
|
|
|
|
#define dll_data_end &_data_end__
|
|
|
|
#define dll_bss_start &_bss_start__
|
|
|
|
#define dll_bss_end &_bss_end__
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2005-12-17 12:36:39 +08:00
|
|
|
|
|
|
|
void
|
2005-12-30 04:46:34 +08:00
|
|
|
child_info_fork::handle_fork ()
|
2005-12-17 12:36:39 +08:00
|
|
|
{
|
|
|
|
cygheap_fixup_in_child (false);
|
2014-10-17 22:36:59 +08:00
|
|
|
memory_init ();
|
2008-12-21 01:32:31 +08:00
|
|
|
myself.thisproc (NULL);
|
2006-03-29 14:35:25 +08:00
|
|
|
myself->uid = cygheap->user.real_uid;
|
|
|
|
myself->gid = cygheap->user.real_gid;
|
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
child_copy (parent, false,
|
2005-12-17 12:36:39 +08:00
|
|
|
"dll data", dll_data_start, dll_data_end,
|
|
|
|
"dll bss", dll_bss_start, dll_bss_end,
|
|
|
|
"user heap", cygheap->user_heap.base, cygheap->user_heap.ptr,
|
|
|
|
NULL);
|
2010-05-08 05:25:19 +08:00
|
|
|
|
2012-03-20 23:07:30 +08:00
|
|
|
/* If my_wr_proc_pipe != NULL then it's a leftover handle from a previously
|
|
|
|
forked process. Close it now or suffer confusion with the parent of our
|
|
|
|
parent. */
|
|
|
|
if (my_wr_proc_pipe)
|
|
|
|
ForceCloseHandle1 (my_wr_proc_pipe, wr_proc_pipe);
|
|
|
|
|
|
|
|
/* Setup our write end of the process pipe. Clear the one in the structure.
|
|
|
|
The destructor should never be called for this but, it can't hurt to be
|
|
|
|
safe. */
|
|
|
|
my_wr_proc_pipe = wr_proc_pipe;
|
|
|
|
rd_proc_pipe = wr_proc_pipe = NULL;
|
2010-05-08 05:25:19 +08:00
|
|
|
/* Do the relocations here. These will actually likely be overwritten by the
|
|
|
|
below child_copy but we do them here in case there is a read-only section
|
|
|
|
which does not get copied by fork. */
|
|
|
|
_pei386_runtime_relocator (user_data);
|
|
|
|
|
2005-12-17 12:36:39 +08:00
|
|
|
/* step 2 now that the dll has its heap filled in, we can fill in the
|
|
|
|
user's data and bss since user_data is now filled out. */
|
2005-12-30 04:46:34 +08:00
|
|
|
child_copy (parent, false,
|
2005-12-17 12:36:39 +08:00
|
|
|
"data", user_data->data_start, user_data->data_end,
|
|
|
|
"bss", user_data->bss_start, user_data->bss_end,
|
|
|
|
NULL);
|
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
if (fixup_mmaps_after_fork (parent))
|
2005-12-17 12:36:39 +08:00
|
|
|
api_fatal ("recreate_mmaps_after_fork_failed");
|
|
|
|
}
|
|
|
|
|
2012-05-15 06:42:56 +08:00
|
|
|
bool
|
|
|
|
child_info_spawn::get_parent_handle ()
|
|
|
|
{
|
|
|
|
parent = OpenProcess (PROCESS_VM_READ, false, parent_winpid);
|
|
|
|
moreinfo->myself_pinfo = NULL;
|
|
|
|
return !!parent;
|
|
|
|
}
|
|
|
|
|
2006-03-21 02:01:17 +08:00
|
|
|
void
|
|
|
|
child_info_spawn::handle_spawn ()
|
|
|
|
{
|
2014-02-10 18:45:51 +08:00
|
|
|
extern void fixup_lockf_after_exec (bool);
|
2006-03-21 02:01:17 +08:00
|
|
|
HANDLE h;
|
2012-05-15 06:42:56 +08:00
|
|
|
if (!dynamically_loaded || get_parent_handle ())
|
|
|
|
{
|
|
|
|
cygheap_fixup_in_child (true);
|
2014-10-17 22:36:59 +08:00
|
|
|
memory_init ();
|
2012-05-15 06:42:56 +08:00
|
|
|
}
|
2006-03-21 02:01:17 +08:00
|
|
|
if (!moreinfo->myself_pinfo ||
|
2009-12-19 04:32:04 +08:00
|
|
|
!DuplicateHandle (GetCurrentProcess (), moreinfo->myself_pinfo,
|
|
|
|
GetCurrentProcess (), &h, 0,
|
2006-03-21 02:01:17 +08:00
|
|
|
FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
|
|
|
h = NULL;
|
2012-03-20 23:07:30 +08:00
|
|
|
|
|
|
|
/* Setup our write end of the process pipe. Clear the one in the structure.
|
|
|
|
The destructor should never be called for this but, it can't hurt to be
|
|
|
|
safe. */
|
|
|
|
my_wr_proc_pipe = wr_proc_pipe;
|
|
|
|
rd_proc_pipe = wr_proc_pipe = NULL;
|
|
|
|
|
2008-12-21 01:32:31 +08:00
|
|
|
myself.thisproc (h);
|
2006-03-21 02:01:17 +08:00
|
|
|
__argc = moreinfo->argc;
|
|
|
|
__argv = moreinfo->argv;
|
|
|
|
envp = moreinfo->envp;
|
|
|
|
envc = moreinfo->envc;
|
|
|
|
if (!dynamically_loaded)
|
|
|
|
cygheap->fdtab.fixup_after_exec ();
|
2006-12-12 02:55:29 +08:00
|
|
|
if (__stdin >= 0)
|
|
|
|
cygheap->fdtab.move_fd (__stdin, 0);
|
|
|
|
if (__stdout >= 0)
|
|
|
|
cygheap->fdtab.move_fd (__stdout, 1);
|
2008-07-09 19:58:38 +08:00
|
|
|
cygheap->user.groups.clear_supp ();
|
2006-12-12 02:55:29 +08:00
|
|
|
|
2011-10-27 03:42:39 +08:00
|
|
|
/* If we're execing we may have "inherited" a list of children forked by the
|
|
|
|
previous process executing under this pid. Reattach them here so that we
|
|
|
|
can wait for them. */
|
2011-11-14 09:29:49 +08:00
|
|
|
if (type == _CH_EXEC)
|
2011-10-27 03:42:39 +08:00
|
|
|
reattach_children ();
|
|
|
|
|
2006-06-03 14:35:10 +08:00
|
|
|
ready (true);
|
2006-06-02 08:09:50 +08:00
|
|
|
|
2012-05-07 23:05:56 +08:00
|
|
|
/* Keep pointer to parent open if we've execed so that pid will not be reused.
|
|
|
|
Otherwise, we no longer need this handle so close it.
|
|
|
|
Need to do this after debug_fixup_after_fork_exec or DEBUGGING handling of
|
2006-06-02 08:09:50 +08:00
|
|
|
handles might get confused. */
|
2012-05-15 06:42:56 +08:00
|
|
|
if (type != _CH_EXEC && child_proc_info->parent)
|
2012-05-07 23:05:56 +08:00
|
|
|
{
|
|
|
|
CloseHandle (child_proc_info->parent);
|
|
|
|
child_proc_info->parent = NULL;
|
|
|
|
}
|
2006-06-02 08:09:50 +08:00
|
|
|
|
2006-03-21 02:01:17 +08:00
|
|
|
signal_fixup_after_exec ();
|
2014-02-10 18:45:51 +08:00
|
|
|
fixup_lockf_after_exec (type == _CH_EXEC);
|
2006-03-21 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2011-06-06 13:02:13 +08:00
|
|
|
/* Retrieve and store system directory for later use. Note that the
|
2010-08-28 16:51:21 +08:00
|
|
|
directory is stored with a trailing backslash! */
|
|
|
|
static void
|
|
|
|
init_windows_system_directory ()
|
|
|
|
{
|
2011-12-14 04:06:31 +08:00
|
|
|
if (!windows_system_directory_length)
|
|
|
|
{
|
|
|
|
windows_system_directory_length =
|
|
|
|
GetSystemDirectoryW (windows_system_directory, MAX_PATH);
|
|
|
|
if (windows_system_directory_length == 0)
|
|
|
|
api_fatal ("can't find windows system directory");
|
|
|
|
windows_system_directory[windows_system_directory_length++] = L'\\';
|
|
|
|
windows_system_directory[windows_system_directory_length] = L'\0';
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2011-12-14 04:06:31 +08:00
|
|
|
system_wow64_directory_length =
|
|
|
|
GetSystemWow64DirectoryW (system_wow64_directory, MAX_PATH);
|
|
|
|
if (system_wow64_directory_length)
|
|
|
|
{
|
|
|
|
system_wow64_directory[system_wow64_directory_length++] = L'\\';
|
|
|
|
system_wow64_directory[system_wow64_directory_length] = L'\0';
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif /* !__x86_64__ */
|
2011-12-14 04:06:31 +08:00
|
|
|
}
|
2010-08-28 16:51:21 +08:00
|
|
|
}
|
|
|
|
|
2011-08-18 23:59:16 +08:00
|
|
|
void
|
2005-05-09 09:19:38 +08:00
|
|
|
dll_crt0_0 ()
|
|
|
|
{
|
2012-03-05 20:09:22 +08:00
|
|
|
wincap.init ();
|
2015-07-20 04:38:30 +08:00
|
|
|
GetModuleFileNameW (NULL, global_progname, NT_MAX_PATH);
|
2011-11-25 05:36:53 +08:00
|
|
|
child_proc_info = get_cygwin_startup_info ();
|
2010-08-28 16:51:21 +08:00
|
|
|
init_windows_system_directory ();
|
2006-03-14 02:29:48 +08:00
|
|
|
initial_env ();
|
|
|
|
|
2008-04-27 02:01:42 +08:00
|
|
|
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
2006-03-21 09:37:25 +08:00
|
|
|
|
2005-10-24 07:47:45 +08:00
|
|
|
lock_process::init ();
|
2005-09-28 23:18:49 +08:00
|
|
|
_impure_ptr = _GLOBAL_REENT;
|
|
|
|
_impure_ptr->_stdin = &_impure_ptr->__sf[0];
|
|
|
|
_impure_ptr->_stdout = &_impure_ptr->__sf[1];
|
|
|
|
_impure_ptr->_stderr = &_impure_ptr->__sf[2];
|
|
|
|
_impure_ptr->_current_locale = "C";
|
2005-12-30 04:46:34 +08:00
|
|
|
user_data->impure_ptr = _impure_ptr;
|
|
|
|
user_data->impure_ptr_ptr = &_impure_ptr;
|
2005-05-09 09:19:38 +08:00
|
|
|
|
2009-12-21 17:38:25 +08:00
|
|
|
DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
|
|
|
|
GetCurrentProcess (), &hMainThread,
|
|
|
|
0, false, DUPLICATE_SAME_ACCESS);
|
|
|
|
|
2011-04-28 17:30:36 +08:00
|
|
|
NtOpenProcessToken (NtCurrentProcess (), MAXIMUM_ALLOWED, &hProcToken);
|
2007-07-19 15:24:17 +08:00
|
|
|
set_cygwin_privileges (hProcToken);
|
2005-05-09 09:19:38 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
device::init ();
|
|
|
|
do_global_ctors (&__CTOR_LIST__, 1);
|
|
|
|
cygthread::init ();
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2002-10-15 15:03:45 +08:00
|
|
|
if (!child_proc_info)
|
2011-12-12 18:16:53 +08:00
|
|
|
{
|
2014-10-17 22:36:59 +08:00
|
|
|
setup_cygheap ();
|
|
|
|
memory_init ();
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2011-12-19 20:50:35 +08:00
|
|
|
/* WOW64 process on XP/64 or Server 2003/64? Check if we have been
|
|
|
|
started from 64 bit process and if our stack is at an unusual
|
|
|
|
address. Set wow64_needs_stack_adjustment if so. Problem
|
|
|
|
description in wow64_test_for_64bit_parent. */
|
|
|
|
if (wincap.wow64_has_secondary_stack ())
|
|
|
|
wow64_needs_stack_adjustment = wow64_test_for_64bit_parent ();
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif /* !__x86_64__ */
|
2011-12-12 18:16:53 +08:00
|
|
|
}
|
2002-10-15 15:03:45 +08:00
|
|
|
else
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2005-05-09 09:19:38 +08:00
|
|
|
cygwin_user_h = child_proc_info->user_h;
|
2001-12-26 12:53:34 +08:00
|
|
|
switch (child_proc_info->type)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2012-03-21 07:13:40 +08:00
|
|
|
case _CH_FORK:
|
|
|
|
fork_info->handle_fork ();
|
|
|
|
break;
|
|
|
|
case _CH_SPAWN:
|
|
|
|
case _CH_EXEC:
|
|
|
|
spawn_info->handle_spawn ();
|
|
|
|
break;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-04-06 00:42:27 +08:00
|
|
|
user_data->threadinterface->Init ();
|
|
|
|
|
2010-10-03 03:08:21 +08:00
|
|
|
_main_tls = &_my_tls;
|
|
|
|
|
2010-09-12 23:49:30 +08:00
|
|
|
/* Initialize signal processing here, early, in the hopes that the creation
|
|
|
|
of a thread early in the process will cause more predictability in memory
|
|
|
|
layout for the main thread. */
|
2011-07-31 04:50:23 +08:00
|
|
|
if (!dynamically_loaded)
|
2010-09-12 23:49:30 +08:00
|
|
|
sigproc_init ();
|
|
|
|
|
2015-07-08 02:45:06 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
/* See comment preceeding myfault_altstack_handler in exception.cc. */
|
|
|
|
AddVectoredContinueHandler (0, myfault_altstack_handler);
|
|
|
|
#endif
|
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
debug_printf ("finished dll_crt0_0 initialization");
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
|
|
|
|
2013-01-09 22:23:44 +08:00
|
|
|
static inline void
|
|
|
|
main_thread_sinit ()
|
|
|
|
{
|
|
|
|
__sinit (_impure_ptr);
|
|
|
|
/* At this point, _impure_ptr == _global_impure_ptr == _GLOBAL_REENT is
|
|
|
|
initialized, but _REENT == _my_tls.local_clib doesn't know about it.
|
|
|
|
It has been copied over from _GLOBAL_REENT in _cygtls::init_thread
|
|
|
|
*before* the initialization took place.
|
|
|
|
|
|
|
|
As soon as the main thread calls a stdio function, this would be
|
|
|
|
rectified. But if another thread calls a stdio function on
|
|
|
|
stdin/out/err before the main thread does, all the required
|
|
|
|
initialization of stdin/out/err will be done, but _REENT->__sdidinit
|
|
|
|
is *still* 0. This in turn will result in a call to __sinit in the
|
|
|
|
wrong spot. The input or output buffer will be NULLed and nothing is
|
|
|
|
read or written in the first stdio function call in the main thread.
|
|
|
|
|
|
|
|
To fix this issue we have to copy over the relevant part of _GLOBAL_REENT
|
|
|
|
to _REENT here again. */
|
|
|
|
_REENT->__sdidinit = -1;
|
|
|
|
_REENT->__cleanup = _GLOBAL_REENT->__cleanup;
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
/* Take over from libc's crt0.o and start the application. Note the
|
|
|
|
various special cases when Cygwin DLL is being runtime loaded (as
|
|
|
|
opposed to being link-time loaded by Cygwin apps) from a non
|
|
|
|
cygwin app via LoadLibrary. */
|
2006-06-02 08:09:50 +08:00
|
|
|
void
|
|
|
|
dll_crt0_1 (void *)
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2009-10-02 22:58:10 +08:00
|
|
|
extern void initial_setlocale ();
|
|
|
|
|
2013-01-22 00:56:09 +08:00
|
|
|
_my_tls.incyg++;
|
2013-04-08 07:53:00 +08:00
|
|
|
/* Inherit "parent" exec'ed process sigmask */
|
|
|
|
if (spawn_info && !in_forkee)
|
|
|
|
_my_tls.sigmask = spawn_info->moreinfo->sigmask;
|
2013-01-22 00:56:09 +08:00
|
|
|
|
2011-07-31 04:50:23 +08:00
|
|
|
if (dynamically_loaded)
|
2008-10-06 00:48:30 +08:00
|
|
|
sigproc_init ();
|
2011-07-31 04:50:23 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
check_sanity_and_sync (user_data);
|
2008-07-26 16:32:28 +08:00
|
|
|
|
2008-07-28 06:31:48 +08:00
|
|
|
/* Initialize malloc and then call user_shared_initialize since it relies
|
|
|
|
on a functioning malloc and it's possible that the user's program may
|
|
|
|
have overridden malloc. We only know about that at this stage,
|
|
|
|
unfortunately. */
|
|
|
|
malloc_init ();
|
2010-03-16 05:29:15 +08:00
|
|
|
user_shared->initialize ();
|
2008-07-26 16:32:28 +08:00
|
|
|
|
2011-02-19 01:43:22 +08:00
|
|
|
#ifdef CYGHEAP_DEBUG
|
2005-08-08 12:06:07 +08:00
|
|
|
int i = 0;
|
2005-08-18 00:10:48 +08:00
|
|
|
const int n = 2 * 1024 * 1024;
|
|
|
|
while (i--)
|
2011-02-19 01:43:22 +08:00
|
|
|
{
|
|
|
|
void *p = cmalloc (HEAP_STR, n);
|
|
|
|
if (p)
|
|
|
|
small_printf ("cmalloc returns %p\n", cmalloc (HEAP_STR, n));
|
|
|
|
else
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
small_printf ("total allocated %y\n", (i - 1) * n);
|
2011-02-19 01:43:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-08-08 12:06:07 +08:00
|
|
|
#endif
|
2004-01-14 23:45:37 +08:00
|
|
|
|
2009-12-21 17:38:25 +08:00
|
|
|
ProtectHandle (hMainThread);
|
|
|
|
|
2008-04-23 04:56:26 +08:00
|
|
|
cygheap->cwd.init ();
|
|
|
|
|
2003-11-29 04:55:59 +08:00
|
|
|
/* Initialize pthread mainthread when not forked and it is safe to call new,
|
2003-05-16 03:42:51 +08:00
|
|
|
otherwise it is reinitalized in fixup_after_fork */
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!in_forkee)
|
2010-05-08 05:25:19 +08:00
|
|
|
{
|
|
|
|
pthread::init_mainthread ();
|
|
|
|
_pei386_runtime_relocator (user_data);
|
|
|
|
}
|
2002-11-24 21:54:14 +08:00
|
|
|
|
2003-07-30 12:46:07 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
strace.microseconds ();
|
|
|
|
#endif
|
|
|
|
|
2002-07-15 03:15:32 +08:00
|
|
|
/* Initialize debug muto, if DLL is built with --enable-debugging.
|
|
|
|
Need to do this before any helper threads start. */
|
|
|
|
debug_init ();
|
|
|
|
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2001-04-19 05:10:15 +08:00
|
|
|
cygheap->fdtab.vfork_child_fixup ();
|
2001-09-15 08:47:44 +08:00
|
|
|
main_vfork = vfork_storage.create ();
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2000-10-19 08:45:39 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
cygbench ("pre-forkee");
|
2005-12-30 04:46:34 +08:00
|
|
|
if (in_forkee)
|
2000-02-18 03:38:33 +08: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
|
|
|
|
frame pointer is referencing the new stack so that the OS knows what
|
|
|
|
to do when it needs to increase the size of the stack.
|
|
|
|
|
|
|
|
NOTE: Don't do anything that involves the stack until you've completed
|
|
|
|
this step. */
|
2011-05-20 15:23:11 +08:00
|
|
|
if (fork_info->stackaddr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
_tlsbase = (PVOID) fork_info->stackbottom;
|
|
|
|
_tlstop = (PVOID) fork_info->stacktop;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2007-02-22 20:34:55 +08:00
|
|
|
|
2013-01-22 01:17:44 +08:00
|
|
|
/* Not resetting _my_tls.incyg here because presumably fork will overwrite
|
|
|
|
it with the value of the forker and all will be good. */
|
2004-09-12 11:47:57 +08:00
|
|
|
longjmp (fork_info->jmp, true);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-01-09 22:23:44 +08:00
|
|
|
main_thread_sinit ();
|
2010-09-20 04:18:36 +08:00
|
|
|
|
2000-11-15 14:27:48 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
{
|
|
|
|
extern void fork_init ();
|
|
|
|
fork_init ();
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-23 00:42:45 +08:00
|
|
|
pinfo_init (envp, envc);
|
2011-06-17 16:30:27 +08:00
|
|
|
strace.dll_info ();
|
2000-11-15 14:27:48 +08:00
|
|
|
|
2001-04-19 05:10:15 +08:00
|
|
|
/* Allocate cygheap->fdtab */
|
2000-08-12 13:35:42 +08:00
|
|
|
dtable_init ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-02-16 23:42:40 +08:00
|
|
|
/* Set internal locale to the environment settings. */
|
|
|
|
initial_setlocale ();
|
|
|
|
|
2006-03-23 00:42:45 +08:00
|
|
|
uinfo_init (); /* initialize user info */
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Connect to tty. */
|
2006-06-04 04:32:07 +08:00
|
|
|
tty::init_session ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!__argc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2008-02-01 04:26:01 +08:00
|
|
|
PWCHAR wline = GetCommandLineW ();
|
2008-02-12 00:39:06 +08:00
|
|
|
size_t size = sys_wcstombs (NULL, 0, wline);
|
2008-02-01 04:26:01 +08:00
|
|
|
char *line = (char *) alloca (size);
|
|
|
|
sys_wcstombs (line, size, wline);
|
2000-12-10 08:45:12 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Scan the command line and build argv. Expand wildcards if not
|
|
|
|
called from another cygwin process. */
|
|
|
|
build_argv (line, __argv, __argc,
|
|
|
|
NOTSTATE (myself, PID_CYGPARENT) && allow_glob);
|
|
|
|
|
|
|
|
/* Convert argv[0] to posix rules if it's currently blatantly
|
|
|
|
win32 style. */
|
|
|
|
if ((strchr (__argv[0], ':')) || (strchr (__argv[0], '\\')))
|
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
char *new_argv0 = (char *) malloc (NT_MAX_PATH);
|
2008-03-12 20:41:50 +08:00
|
|
|
cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, __argv[0],
|
|
|
|
new_argv0, NT_MAX_PATH);
|
2003-12-26 12:40:52 +08:00
|
|
|
__argv[0] = (char *) realloc (new_argv0, strlen (new_argv0) + 1);
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-09 11:11:31 +08:00
|
|
|
__argc_safe = __argc;
|
2000-09-03 12:16:35 +08:00
|
|
|
if (user_data->premain[0])
|
|
|
|
for (unsigned int i = 0; i < PREMAIN_LEN / 2; i++)
|
2001-03-05 14:28:25 +08:00
|
|
|
user_data->premain[i] (__argc, __argv, user_data);
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2001-03-28 11:42:58 +08:00
|
|
|
/* Set up standard fds in file descriptor table. */
|
2002-02-23 03:33:41 +08:00
|
|
|
cygheap->fdtab.stdio_init ();
|
2001-03-28 11:42:58 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Set up __progname for getopt error call. */
|
2002-06-28 00:01:38 +08:00
|
|
|
if (__argv[0] && (__progname = strrchr (__argv[0], '/')))
|
|
|
|
++__progname;
|
|
|
|
else
|
|
|
|
__progname = __argv[0];
|
2010-12-27 05:11:38 +08:00
|
|
|
program_invocation_name = __argv[0];
|
|
|
|
program_invocation_short_name = __progname;
|
2002-06-28 00:01:38 +08:00
|
|
|
if (__progname)
|
|
|
|
{
|
|
|
|
char *cp = strchr (__progname, '\0') - 4;
|
2008-02-01 20:37:51 +08:00
|
|
|
if (cp > __progname && ascii_strcasematch (cp, ".exe"))
|
2002-06-28 00:01:38 +08:00
|
|
|
*cp = '\0';
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2010-03-03 23:05:19 +08:00
|
|
|
(void) xdr_set_vprintf (&cygxdr_vwarnx);
|
2004-02-12 11:01:58 +08:00
|
|
|
cygwin_finished_initializing = true;
|
2000-07-15 10:48:11 +08:00
|
|
|
/* Call init of loaded dlls. */
|
|
|
|
dlls.init ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-01 11:51:55 +08:00
|
|
|
/* Execute any specified "premain" functions */
|
2000-07-06 07:46:44 +08:00
|
|
|
if (user_data->premain[PREMAIN_LEN / 2])
|
2000-07-01 11:51:55 +08:00
|
|
|
for (unsigned int i = PREMAIN_LEN / 2; i < PREMAIN_LEN; i++)
|
2001-03-05 14:28:25 +08:00
|
|
|
user_data->premain[i] (__argc, __argv, user_data);
|
2000-07-01 11:51:55 +08:00
|
|
|
|
2010-09-12 23:49:30 +08:00
|
|
|
set_errno (0);
|
2000-07-15 10:48:11 +08:00
|
|
|
|
2000-07-18 03:18:21 +08:00
|
|
|
if (dynamically_loaded)
|
2011-05-05 15:31:13 +08:00
|
|
|
{
|
|
|
|
_setlocale_r (_REENT, LC_CTYPE, "C");
|
|
|
|
return;
|
|
|
|
}
|
2000-07-18 03:18:21 +08:00
|
|
|
|
2000-11-11 13:36:34 +08:00
|
|
|
/* Disable case-insensitive globbing */
|
2003-12-08 06:37:12 +08:00
|
|
|
ignore_case_with_glob = false;
|
2000-11-11 13:36:34 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
MALLOC_CHECK;
|
2000-10-17 07:55:58 +08:00
|
|
|
cygbench (__progname);
|
2003-12-24 00:26:31 +08:00
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
ld_preload ();
|
2010-01-15 02:56:24 +08:00
|
|
|
/* Per POSIX set the default application locale back to "C". */
|
|
|
|
_setlocale_r (_REENT, LC_CTYPE, "C");
|
2010-09-12 23:49:30 +08:00
|
|
|
|
2013-01-22 00:56:09 +08:00
|
|
|
if (!user_data->main)
|
|
|
|
{
|
|
|
|
/* Handle any signals which may have arrived */
|
|
|
|
_my_tls.call_signal_handler ();
|
|
|
|
_my_tls.incyg--; /* Not in Cygwin anymore */
|
|
|
|
}
|
|
|
|
else
|
2011-10-18 02:25:04 +08:00
|
|
|
{
|
|
|
|
/* Create a copy of Cygwin's version of __argv so that, if the user makes
|
|
|
|
a change to an element of argv[] it does not affect Cygwin's argv.
|
|
|
|
Changing the the contents of what argv[n] points to will still
|
|
|
|
affect Cygwin. This is similar (but not exactly like) Linux. */
|
|
|
|
char *newargv[__argc + 1];
|
|
|
|
char **nav = newargv;
|
|
|
|
char **oav = __argv;
|
|
|
|
while ((*nav++ = *oav++) != NULL)
|
|
|
|
continue;
|
2013-01-22 00:56:09 +08:00
|
|
|
/* Handle any signals which may have arrived */
|
2013-03-31 20:35:44 +08:00
|
|
|
sig_dispatch_pending (false);
|
2013-01-22 00:56:09 +08:00
|
|
|
_my_tls.call_signal_handler ();
|
|
|
|
_my_tls.incyg--; /* Not in Cygwin anymore */
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
cygwin_exit (user_data->main (__argc, newargv, __cygwin_environ));
|
|
|
|
#else
|
2011-10-18 02:25:04 +08:00
|
|
|
cygwin_exit (user_data->main (__argc, newargv, *user_data->envptr));
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2011-10-18 02:25:04 +08:00
|
|
|
}
|
2008-08-02 03:12:49 +08:00
|
|
|
__asm__ (" \n\
|
2013-04-23 17:44:36 +08:00
|
|
|
.global _cygwin_exit_return \n\
|
2008-08-02 03:12:49 +08:00
|
|
|
.global __cygwin_exit_return \n\
|
2013-04-23 17:44:36 +08:00
|
|
|
_cygwin_exit_return: \n\
|
2008-08-02 03:12:49 +08:00
|
|
|
__cygwin_exit_return: \n\
|
2013-04-23 17:44:36 +08:00
|
|
|
nop \n\
|
2008-08-02 03:12:49 +08:00
|
|
|
");
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-01 12:26:45 +08:00
|
|
|
extern "C" void __stdcall
|
|
|
|
_dll_crt0 ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2011-12-19 20:50:35 +08:00
|
|
|
/* Handle WOW64 process on XP/2K3 which has been started from native 64 bit
|
|
|
|
process. See comment in wow64_test_for_64bit_parent for a full problem
|
|
|
|
description. */
|
|
|
|
if (wow64_needs_stack_adjustment && !dynamically_loaded)
|
2011-12-16 19:58:03 +08:00
|
|
|
{
|
2011-12-22 01:19:48 +08:00
|
|
|
/* Must be static since it's referenced after the stack and frame
|
|
|
|
pointer registers have been changed. */
|
2011-12-16 19:58:03 +08:00
|
|
|
static PVOID allocationbase = 0;
|
|
|
|
|
2011-12-22 01:19:48 +08:00
|
|
|
/* Check if we just move the stack. If so, wow64_revert_to_original_stack
|
|
|
|
returns a non-NULL, 16 byte aligned address. See comments in
|
2011-12-16 19:58:03 +08:00
|
|
|
wow64_revert_to_original_stack for the gory details. */
|
|
|
|
PVOID stackaddr = wow64_revert_to_original_stack (allocationbase);
|
|
|
|
if (stackaddr)
|
|
|
|
{
|
2011-12-22 01:19:48 +08:00
|
|
|
/* 2nd half of the stack move. Set stack pointer to new address.
|
|
|
|
Set frame pointer to 0. */
|
2011-12-16 19:58:03 +08:00
|
|
|
__asm__ ("\n\
|
|
|
|
movl %[ADDR], %%esp \n\
|
2011-12-22 01:19:48 +08:00
|
|
|
xorl %%ebp, %%ebp \n"
|
2011-12-16 19:58:03 +08:00
|
|
|
: : [ADDR] "r" (stackaddr));
|
|
|
|
/* Now we're back on the original stack. Free up space taken by the
|
2011-12-17 18:24:09 +08:00
|
|
|
former main thread stack and set DeallocationStack correctly. */
|
|
|
|
VirtualFree (NtCurrentTeb ()->DeallocationStack, 0, MEM_RELEASE);
|
2011-12-16 19:58:03 +08:00
|
|
|
NtCurrentTeb ()->DeallocationStack = allocationbase;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Fall back to respawn if wow64_revert_to_original_stack fails. */
|
|
|
|
wow64_respawn_process ();
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif /* !__x86_64__ */
|
2011-03-17 16:40:25 +08:00
|
|
|
_feinitialise ();
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2000-07-15 10:48:11 +08:00
|
|
|
main_environ = user_data->envptr;
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2005-12-30 04:46:34 +08:00
|
|
|
if (in_forkee)
|
2010-10-03 03:08:21 +08:00
|
|
|
{
|
|
|
|
fork_info->alloc_stack ();
|
|
|
|
_main_tls = &_my_tls;
|
|
|
|
}
|
2004-01-27 02:52:02 +08:00
|
|
|
|
2006-05-25 10:33:13 +08:00
|
|
|
_main_tls->call ((DWORD (*) (void *, void *)) dll_crt0_1, NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-01 11:51:55 +08:00
|
|
|
void
|
|
|
|
dll_crt0 (per_process *uptr)
|
|
|
|
{
|
|
|
|
/* Set the local copy of the pointer into the user space. */
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!in_forkee && uptr && uptr != user_data)
|
2000-07-06 07:46:44 +08:00
|
|
|
{
|
|
|
|
memcpy (user_data, uptr, per_process_overwrite);
|
2004-09-16 11:49:03 +08:00
|
|
|
*(user_data->impure_ptr_ptr) = _GLOBAL_REENT;
|
2000-07-06 07:46:44 +08:00
|
|
|
}
|
2000-07-01 12:26:45 +08:00
|
|
|
_dll_crt0 ();
|
2000-07-01 11:51:55 +08:00
|
|
|
}
|
|
|
|
|
2005-06-04 10:11:50 +08:00
|
|
|
/* This must be called by anyone who uses LoadLibrary to load cygwin1.dll.
|
2005-07-29 01:03:34 +08:00
|
|
|
You must have CYGTLS_PADSIZE bytes reserved at the bottom of the stack
|
|
|
|
calling this function, and that storage must not be overwritten until you
|
|
|
|
unload cygwin1.dll, as it is used for _my_tls. It is best to load
|
|
|
|
cygwin1.dll before spawning any additional threads in your process.
|
2005-08-12 10:39:13 +08:00
|
|
|
|
2005-07-29 01:03:34 +08:00
|
|
|
See winsup/testsuite/cygload for an example of how to use cygwin1.dll
|
|
|
|
from MSVC and non-cygwin MinGW applications. */
|
2000-07-06 07:46:44 +08:00
|
|
|
extern "C" void
|
2000-06-08 08:55:27 +08:00
|
|
|
cygwin_dll_init ()
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2000-06-08 08:55:27 +08:00
|
|
|
static char **envp;
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2000-06-08 08:55:27 +08:00
|
|
|
static int _fmode;
|
|
|
|
|
|
|
|
user_data->magic_biscuit = sizeof (per_process);
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifndef __x86_64__
|
2000-06-08 08:55:27 +08:00
|
|
|
user_data->envptr = &envp;
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2000-06-08 08:55:27 +08:00
|
|
|
user_data->fmode_ptr = &_fmode;
|
|
|
|
|
2006-01-02 23:48:29 +08:00
|
|
|
_dll_crt0 ();
|
2000-06-08 08:55:27 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
|
|
|
__main (void)
|
|
|
|
{
|
2009-07-30 23:48:51 +08:00
|
|
|
/* Ordering is critical here. DLL ctors have already been
|
2009-08-04 12:20:36 +08:00
|
|
|
run as they were being loaded, so we should stack the
|
2009-07-30 23:48:51 +08:00
|
|
|
queued call to DLL dtors now. */
|
|
|
|
atexit (dll_global_dtors);
|
2003-12-08 06:37:12 +08:00
|
|
|
do_global_ctors (user_data->ctors, false);
|
2010-09-20 04:18:36 +08:00
|
|
|
/* Now we have run global ctors, register their dtors.
|
|
|
|
|
|
|
|
At exit, global dtors will run first, so the app can still
|
2009-07-30 23:48:51 +08:00
|
|
|
use shared library functions while terminating; then the
|
|
|
|
DLLs will be destroyed; finally newlib will shut down stdio
|
|
|
|
and terminate itself. */
|
2010-09-20 04:18:36 +08:00
|
|
|
atexit (do_global_dtors);
|
|
|
|
sig_dispatch_pending (true);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
void __reg1
|
2000-02-18 03:38:33 +08:00
|
|
|
do_exit (int status)
|
|
|
|
{
|
2003-12-10 11:19:19 +08:00
|
|
|
syscall_printf ("do_exit (%d), exit_state %d", status, exit_state);
|
|
|
|
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2003-12-10 11:19:19 +08:00
|
|
|
vfork_save *vf = vfork_storage.val ();
|
|
|
|
if (vf != NULL && vf->pid < 0)
|
|
|
|
{
|
|
|
|
exit_state = ES_NOT_EXITING;
|
|
|
|
vf->restore_exit (status);
|
|
|
|
}
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2003-12-10 11:19:19 +08:00
|
|
|
|
2005-10-18 07:27:00 +08:00
|
|
|
lock_process until_exit (true);
|
2005-06-08 02:41:31 +08:00
|
|
|
|
2003-09-23 12:02:07 +08:00
|
|
|
if (exit_state < ES_EVENTS_TERMINATE)
|
2012-12-29 02:06:17 +08:00
|
|
|
exit_state = ES_EVENTS_TERMINATE;
|
2003-09-23 12:02:07 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (exit_state < ES_SIGNAL)
|
|
|
|
{
|
|
|
|
exit_state = ES_SIGNAL;
|
2004-11-26 12:15:10 +08:00
|
|
|
signal (SIGCHLD, SIG_IGN);
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
signal (SIGQUIT, SIG_IGN);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
if (exit_state < ES_CLOSEALL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
exit_state = ES_CLOSEALL;
|
2005-06-02 10:36:50 +08:00
|
|
|
close_all_files ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-10-25 02:50:57 +08:00
|
|
|
UINT n = (UINT) status;
|
|
|
|
if (exit_state < ES_THREADTERM)
|
|
|
|
{
|
|
|
|
exit_state = ES_THREADTERM;
|
|
|
|
cygthread::terminate ();
|
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
myself->stopsig = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
if (exit_state < ES_HUP_PGRP)
|
|
|
|
{
|
|
|
|
exit_state = ES_HUP_PGRP;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Kill orphaned children on group leader exit */
|
2000-10-25 11:54:50 +08:00
|
|
|
if (myself->has_pgid_children && myself->pid == myself->pgid)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-02-07 02:24:11 +08:00
|
|
|
siginfo_t si = {0};
|
2004-01-19 13:46:54 +08:00
|
|
|
si.si_signo = -SIGHUP;
|
|
|
|
si.si_code = SI_KERNEL;
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("%u == pgrp %u, send SIG{HUP,CONT} to stopped children",
|
2000-02-18 03:38:33 +08:00
|
|
|
myself->pid, myself->pgid);
|
2004-01-19 13:46:54 +08:00
|
|
|
kill_pgrp (myself->pgid, si);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2002-10-14 02:16:33 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
if (exit_state < ES_HUP_SID)
|
|
|
|
{
|
|
|
|
exit_state = ES_HUP_SID;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Kill the foreground process group on session leader exit */
|
2001-04-29 07:48:28 +08:00
|
|
|
if (getpgrp () > 0 && myself->pid == myself->sid && real_tty_attached (myself))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
tty *tp = cygwin_shared->tty[myself->ctty];
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("%u == sid %u, send SIGHUP to children",
|
2000-02-18 03:38:33 +08:00
|
|
|
myself->pid, myself->sid);
|
|
|
|
|
2000-10-25 22:18:56 +08:00
|
|
|
/* CGF FIXME: This can't be right. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (tp->getsid () == myself->sid)
|
2002-02-23 03:33:41 +08:00
|
|
|
tp->kill_pgrp (SIGHUP);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-10-12 12:38:29 +08:00
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
}
|
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
myself.exit (n);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2014-10-29 17:56:18 +08:00
|
|
|
/* When introducing support for -fuse-cxa-atexit with Cygwin 1.7.32 and
|
|
|
|
GCC 4.8.3-3, we defined __dso_value as &ImageBase. This supposedly allowed
|
|
|
|
a reproducible value which could also be easily evaluated in cygwin_atexit.
|
|
|
|
However, when building C++ applications with -fuse-cxa-atexit, G++ creates
|
|
|
|
calls to __cxa_atexit using the *address* of __dso_handle as DSO handle.
|
|
|
|
|
|
|
|
So what we do here is this: A call to __cxa_atexit from the application
|
2014-11-07 16:33:22 +08:00
|
|
|
actually calls cygwin__cxa_atexit. From dso_handle (which is either
|
|
|
|
&__dso_handle, or __dso_handle == ImageBase or NULL) we fetch the dll
|
|
|
|
structure of the DLL. Then use dll::handle == ImageBase as the actual DSO
|
|
|
|
handle value in calls to __cxa_atexit and __cxa_finalize.
|
|
|
|
Thus, __cxa_atexit becomes entirely independent of the incoming value of
|
|
|
|
dso_handle, as long as it's *some* pointer into the DSO's address space. */
|
2014-10-29 17:56:18 +08:00
|
|
|
extern "C" int
|
|
|
|
cygwin__cxa_atexit (void (*fn)(void *), void *obj, void *dso_handle)
|
|
|
|
{
|
2014-11-07 16:33:22 +08:00
|
|
|
dll *d = dso_handle ? dlls.find (dso_handle) : NULL;
|
|
|
|
return __cxa_atexit (fn, obj, d ? d->handle : NULL);
|
2014-10-29 17:56:18 +08:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:48:00 +08:00
|
|
|
/* This function is only called for applications built with Cygwin versions
|
2014-11-06 23:32:21 +08:00
|
|
|
up to API 0.279. Starting with API 0.280 (Cygwin 1.7.33/1.8.6-2), atexit
|
|
|
|
is a statically linked function inside of libcygwin.a. The reason is that
|
|
|
|
the old method to fetch the caller return address is unreliable given GCCs
|
|
|
|
ability to perform tail call elimination. For the details, see the below
|
|
|
|
comment. The atexit replacement is defined in libcygwin.a to allow reliable
|
|
|
|
access to the correct DSO handle. */
|
2003-03-01 10:02:42 +08:00
|
|
|
extern "C" int
|
2010-02-02 10:00:01 +08:00
|
|
|
cygwin_atexit (void (*fn) (void))
|
2003-03-01 10:02:42 +08:00
|
|
|
{
|
|
|
|
int res;
|
2014-10-28 05:19:40 +08:00
|
|
|
|
2010-02-02 10:00:01 +08:00
|
|
|
dll *d = dlls.find ((void *) _my_tls.retaddr ());
|
2014-10-28 05:19:40 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
/* x86_64 DLLs created with GCC 4.8.3-3 register __gcc_deregister_frame
|
|
|
|
as atexit function using a call to atexit, rather than __cxa_atexit.
|
2014-11-05 17:48:00 +08:00
|
|
|
Due to GCC's tail call optimizing, cygwin_atexit doesn't get the correct
|
2014-10-28 05:19:40 +08:00
|
|
|
return address on the stack. As a result it fails to get the HMODULE of
|
|
|
|
the caller and thus calls atexit rather than __cxa_atexit. Then, if the
|
|
|
|
module gets dlclosed, __cxa_finalize (called from dll_list::detach) can't
|
|
|
|
remove __gcc_deregister_frame from the atexit function chain. So at
|
|
|
|
process exit, __call_exitprocs calls __gcc_deregister_frame while the
|
|
|
|
module is already unloaded and the __gcc_deregister_frame function not
|
|
|
|
available ==> SEGV.
|
|
|
|
|
2014-11-05 17:48:00 +08:00
|
|
|
This also occurs for other functions.
|
|
|
|
|
|
|
|
Workaround: If dlls.find fails, try to find the dll entry of the DLL
|
|
|
|
containing fn. If that works, proceed by calling __cxa_atexit, otherwise
|
|
|
|
call atexit.
|
|
|
|
|
|
|
|
This *should* be sufficiently safe. Ultimately, new applications will
|
|
|
|
use the statically linked atexit function though, as outlined above. */
|
|
|
|
if (!d)
|
|
|
|
d = dlls.find ((void *) fn);
|
2014-10-28 05:19:40 +08:00
|
|
|
#endif
|
2014-08-08 00:25:06 +08:00
|
|
|
res = d ? __cxa_atexit ((void (*) (void *)) fn, NULL, d->handle) : atexit (fn);
|
2003-03-01 10:02:42 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
cygwin_exit (int n)
|
|
|
|
{
|
2010-03-29 01:27:52 +08:00
|
|
|
exit_state = ES_EXIT_STARTING;
|
2003-03-01 10:02:42 +08:00
|
|
|
exit (n);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
|
|
|
_exit (int n)
|
|
|
|
{
|
2004-11-26 12:15:10 +08:00
|
|
|
do_exit (((DWORD) n & 0xff) << 8);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 01:09:33 +08:00
|
|
|
extern "C" void cygwin_stackdump ();
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
2011-05-29 02:17:09 +08:00
|
|
|
vapi_fatal (const char *fmt, va_list ap)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char buf[4096];
|
2011-05-29 02:17:09 +08:00
|
|
|
int n = __small_sprintf (buf, "%P: *** fatal error %s- ", in_forkee ? "in forked process " : "");
|
2004-01-14 23:45:37 +08:00
|
|
|
__small_vsprintf (buf + n, fmt, ap);
|
2000-02-18 03:38:33 +08:00
|
|
|
va_end (ap);
|
2005-12-27 03:34:59 +08:00
|
|
|
strace.prntf (_STRACE_SYSTEM, NULL, "%s", buf);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
2005-07-07 04:05:03 +08:00
|
|
|
try_to_debug ();
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
2010-06-22 01:09:33 +08:00
|
|
|
cygwin_stackdump ();
|
2006-03-14 05:10:14 +08:00
|
|
|
myself.exit (__api_fatal_exit_val);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-29 02:17:09 +08:00
|
|
|
extern "C" void
|
|
|
|
api_fatal (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vapi_fatal (fmt, ap);
|
|
|
|
}
|
|
|
|
|
2001-10-10 07:28:24 +08:00
|
|
|
void
|
2013-04-23 17:44:36 +08:00
|
|
|
multiple_cygwin_problem (const char *what, uintptr_t magic_version, uintptr_t version)
|
2001-10-10 07:28:24 +08:00
|
|
|
{
|
2002-08-27 12:10:54 +08:00
|
|
|
if (_cygwin_testing && (strstr (what, "proc") || strstr (what, "cygheap")))
|
2001-12-27 05:35:16 +08:00
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
child_proc_info->type = _CH_WHOOPS;
|
2001-12-27 05:35:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2002-01-14 00:55:32 +08:00
|
|
|
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_MISMATCH_OK", NULL, 0))
|
2002-01-14 00:55:32 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (CYGWIN_VERSION_MAGIC_VERSION (magic_version) == version)
|
2013-04-23 17:44:36 +08:00
|
|
|
system_printf ("%s magic number mismatch detected - %p/%ly", what, magic_version, version);
|
2002-01-14 00:55:32 +08:00
|
|
|
else
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("%s mismatch detected - %ly/%ly.\n\
|
2005-01-06 05:40:08 +08:00
|
|
|
This problem is probably due to using incompatible versions of the cygwin DLL.\n\
|
2001-10-10 07:28:24 +08:00
|
|
|
Search for cygwin1.dll using the Windows Start->Find/Search facility\n\
|
2001-12-26 12:53:34 +08:00
|
|
|
and delete all but the most recent version. The most recent version *should*\n\
|
|
|
|
reside in x:\\cygwin\\bin, where 'x' is the drive on which you have\n\
|
2005-01-06 05:40:08 +08:00
|
|
|
installed the cygwin distribution. Rebooting is also suggested if you\n\
|
|
|
|
are unable to find another cygwin DLL.",
|
|
|
|
what, magic_version, version);
|
2001-10-10 07:28:24 +08:00
|
|
|
}
|
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
#ifdef DEBUGGING
|
2013-07-18 01:46:10 +08:00
|
|
|
void __reg1
|
2000-10-17 07:55:58 +08:00
|
|
|
cygbench (const char *s)
|
|
|
|
{
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_BENCH", NULL, 0))
|
2013-04-23 17:44:36 +08:00
|
|
|
small_printf ("%05u ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
|
2000-10-17 07:55:58 +08:00
|
|
|
}
|
|
|
|
#endif
|