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,
|
2010-01-02 03:28:15 +08:00
|
|
|
2007, 2008, 2009, 2010
|
2009-01-03 13:12:22 +08:00
|
|
|
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>
|
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"
|
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-01-03 13:12:22 +08:00
|
|
|
static char title_buf[TITLESIZE + 1];
|
2000-02-18 03:38:33 +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,
|
|
|
|
GENERIC_READ, /* open for reading */
|
|
|
|
FILE_SHARE_READ, /* 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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
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);
|
2003-09-14 09:58:03 +08:00
|
|
|
if (*(p = strechr (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. */
|
|
|
|
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;
|
2000-05-30 08:38:51 +08:00
|
|
|
int dos_spec = isdrive (word);
|
2001-03-03 11:56:34 +08:00
|
|
|
if (!dos_spec && isquote (*word) && word[1] && word[2])
|
2000-05-30 08:38:51 +08:00
|
|
|
dos_spec = isdrive (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;
|
|
|
|
while (cnt-- > 0)
|
|
|
|
*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;
|
2000-07-16 12:37:43 +08:00
|
|
|
cmd = quoted (cmd, winshell && argc > 0);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
api_fatal ("cygwin DLL and APP are out of sync -- DLL version mismatch %d < %d",
|
|
|
|
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)
|
|
|
|
api_fatal ("Incompatible cygwin .dll -- incompatible per_process info %d != %d",
|
|
|
|
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)
|
|
|
|
api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %d > %d",
|
2000-02-18 03:38:33 +08:00
|
|
|
p->api_major, cygwin_version.api_major);
|
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;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-06-16 05:59:32 +08:00
|
|
|
child_info NO_COPY *child_proc_info = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2007-02-22 20:34:55 +08:00
|
|
|
#define CYGWIN_GUARD (PAGE_EXECUTE_READWRITE | PAGE_GUARD)
|
2001-07-29 10:33:30 +08:00
|
|
|
|
2006-04-04 01:33:07 +08:00
|
|
|
void
|
|
|
|
child_info_fork::alloc_stack_hard_way (volatile char *b)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
void *new_stack_pointer;
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
2000-09-03 12:16:35 +08:00
|
|
|
void *newbase;
|
|
|
|
int newlen;
|
2007-02-22 20:34:55 +08:00
|
|
|
bool guard;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-04-04 00:04:02 +08:00
|
|
|
if (!VirtualQuery ((LPCVOID) &b, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get stack info, %E");
|
|
|
|
|
|
|
|
LPBYTE curbot = (LPBYTE) m.BaseAddress + m.RegionSize;
|
|
|
|
|
2006-04-04 01:33:07 +08:00
|
|
|
if (stacktop > (LPBYTE) m.AllocationBase && stacktop < curbot)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
newbase = curbot;
|
2006-04-04 01:33:07 +08:00
|
|
|
newlen = (LPBYTE) stackbottom - (LPBYTE) curbot;
|
2007-02-22 20:34:55 +08:00
|
|
|
guard = false;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-22 20:34:55 +08:00
|
|
|
newbase = (LPBYTE) stacktop - (128 * 1024);
|
|
|
|
newlen = (LPBYTE) stackbottom - (LPBYTE) newbase;
|
|
|
|
guard = true;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2007-02-22 20:34:55 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!VirtualAlloc (newbase, newlen, MEM_RESERVE, PAGE_NOACCESS))
|
2000-02-18 03:38:33 +08:00
|
|
|
api_fatal ("fork: can't reserve memory for stack %p - %p, %E",
|
2006-04-04 01:33:07 +08:00
|
|
|
stacktop, stackbottom);
|
2007-02-22 20:34:55 +08:00
|
|
|
new_stack_pointer = (void *) ((LPBYTE) stackbottom - (stacksize += 8192));
|
2006-04-04 01:33:07 +08:00
|
|
|
if (!VirtualAlloc (new_stack_pointer, stacksize, MEM_COMMIT,
|
2000-02-18 03:38:33 +08:00
|
|
|
PAGE_EXECUTE_READWRITE))
|
|
|
|
api_fatal ("fork: can't commit memory for stack %p(%d), %E",
|
2006-04-04 01:33:07 +08:00
|
|
|
new_stack_pointer, stacksize);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!VirtualQuery ((LPCVOID) new_stack_pointer, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get new stack info, %E");
|
2007-02-22 20:34:55 +08:00
|
|
|
|
|
|
|
if (guard)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2007-02-22 20:34:55 +08:00
|
|
|
m.BaseAddress = (LPBYTE) m.BaseAddress - 1;
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!VirtualAlloc ((LPVOID) m.BaseAddress, 1, MEM_COMMIT,
|
2002-08-16 04:34:49 +08:00
|
|
|
CYGWIN_GUARD))
|
2000-09-03 12:16:35 +08:00
|
|
|
api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
|
|
|
|
m.BaseAddress);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!VirtualQuery ((LPCVOID) m.BaseAddress, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get new stack info, %E");
|
2006-04-04 01:33:07 +08:00
|
|
|
stacktop = m.BaseAddress;
|
2004-02-14 12:38:37 +08:00
|
|
|
b[0] = '\0';
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-09-28 23:18:49 +08:00
|
|
|
void *getstack (void *) __attribute__ ((noinline));
|
|
|
|
volatile char *
|
2005-12-02 11:12:06 +08:00
|
|
|
getstack (volatile char * volatile p)
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
2005-12-02 11:12:06 +08:00
|
|
|
*p ^= 1;
|
|
|
|
*p ^= 1;
|
2005-09-28 23:18:49 +08:00
|
|
|
return p - 4096;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* extend the stack prior to fork longjmp */
|
|
|
|
|
2006-04-04 01:33:07 +08:00
|
|
|
void
|
|
|
|
child_info_fork::alloc_stack ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-12-02 11:12:06 +08:00
|
|
|
volatile char * volatile esp;
|
2005-09-28 23:18:49 +08:00
|
|
|
__asm__ volatile ("movl %%esp,%0": "=r" (esp));
|
2006-04-04 01:33:07 +08:00
|
|
|
if (_tlsbase != stackbottom)
|
|
|
|
alloc_stack_hard_way (esp);
|
2004-02-14 12:38:37 +08:00
|
|
|
else
|
2005-09-28 23:18:49 +08:00
|
|
|
{
|
2006-04-04 01:33:07 +08:00
|
|
|
char *st = (char *) stacktop - 4096;
|
|
|
|
while (_tlstop >= st)
|
2005-09-28 23:18:49 +08:00
|
|
|
esp = getstack (esp);
|
2006-04-04 01:33:07 +08:00
|
|
|
stacksize = 0;
|
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
|
2008-02-26 02:32:23 +08:00
|
|
|
char buf[NT_MAX_PATH];
|
2004-01-14 23:45:37 +08:00
|
|
|
DWORD len;
|
2004-01-20 07:03:43 +08:00
|
|
|
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
|
|
|
DWORD ms = atoi (buf);
|
2007-12-12 20:12:24 +08:00
|
|
|
console_printf ("Sleeping %d, pid %u %P\n", ms, GetCurrentProcessId ());
|
2004-01-19 13:46:54 +08:00
|
|
|
Sleep (ms);
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!strace.active () && !dynamically_loaded)
|
|
|
|
strace.hello ();
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
char buf1[NT_MAX_PATH];
|
|
|
|
len = GetModuleFileName (NULL, buf1, NT_MAX_PATH);
|
2004-01-14 23:45:37 +08:00
|
|
|
strlwr (buf1);
|
|
|
|
strlwr (buf);
|
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';
|
|
|
|
if (strstr (buf1, buf))
|
|
|
|
{
|
|
|
|
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-02-18 03:38:33 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
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)
|
2005-05-09 09:19:38 +08:00
|
|
|
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)
|
|
|
|
multiple_cygwin_problem ("cygheap base", (DWORD) res->cygheap,
|
2004-01-14 23:45:37 +08:00
|
|
|
(DWORD) &_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
|
|
|
{
|
|
|
|
case _PROC_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 */;
|
|
|
|
case _PROC_SPAWN:
|
|
|
|
case _PROC_EXEC:
|
|
|
|
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)
|
|
|
|
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
|
|
|
{
|
|
|
|
res->ready (false);
|
|
|
|
for (unsigned i = 0; !being_debugged () && i < 10000; i++)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2005-12-30 04:46:34 +08:00
|
|
|
strace.hello ();
|
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
break;
|
2004-01-14 23:45:37 +08:00
|
|
|
default:
|
2005-05-09 09:19:38 +08:00
|
|
|
system_printf ("unknown exec type %d", res->type);
|
2004-01-14 23:45:37 +08:00
|
|
|
/* intentionally fall through */
|
|
|
|
case _PROC_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;
|
|
|
|
}
|
|
|
|
|
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__
|
|
|
|
|
|
|
|
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);
|
2009-06-08 11:53:40 +08:00
|
|
|
memory_init (false);
|
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
|
|
|
|
|
|
|
/* 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");
|
|
|
|
}
|
|
|
|
|
2006-03-21 02:01:17 +08:00
|
|
|
void
|
|
|
|
child_info_spawn::handle_spawn ()
|
|
|
|
{
|
2008-03-24 22:48:58 +08:00
|
|
|
extern void fixup_lockf_after_exec ();
|
2006-03-21 02:01:17 +08:00
|
|
|
HANDLE h;
|
|
|
|
cygheap_fixup_in_child (true);
|
2009-06-08 11:53:40 +08:00
|
|
|
memory_init (false);
|
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;
|
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
|
|
|
|
2006-06-03 14:35:10 +08:00
|
|
|
ready (true);
|
2006-06-02 08:09:50 +08:00
|
|
|
|
|
|
|
/* Need to do this after debug_fixup_after_fork_exec or DEBUGGING handling of
|
|
|
|
handles might get confused. */
|
|
|
|
CloseHandle (child_proc_info->parent);
|
|
|
|
child_proc_info->parent = NULL;
|
|
|
|
|
2006-03-21 02:01:17 +08:00
|
|
|
signal_fixup_after_exec ();
|
|
|
|
if (moreinfo->old_title)
|
|
|
|
{
|
|
|
|
old_title = strcpy (title_buf, moreinfo->old_title);
|
|
|
|
cfree (moreinfo->old_title);
|
|
|
|
}
|
2008-03-24 22:48:58 +08:00
|
|
|
fixup_lockf_after_exec ();
|
2006-03-21 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2009-03-04 19:45:58 +08:00
|
|
|
#if 0
|
|
|
|
/* Setting the TS-aware flag in the application's PE header is sufficient.
|
|
|
|
Just keep this in as a reminder. */
|
|
|
|
|
2008-11-12 19:04:27 +08:00
|
|
|
static DEP_SYSTEM_POLICY_TYPE dep_system_policy = (DEP_SYSTEM_POLICY_TYPE) -1;
|
|
|
|
|
|
|
|
static void
|
|
|
|
disable_dep ()
|
|
|
|
{
|
|
|
|
DWORD ppolicy;
|
|
|
|
BOOL perm;
|
|
|
|
|
|
|
|
if (dep_system_policy < 0)
|
|
|
|
{
|
|
|
|
dep_system_policy = GetSystemDEPPolicy ();
|
|
|
|
debug_printf ("DEP System Policy: %d", (int) dep_system_policy);
|
|
|
|
}
|
|
|
|
if (dep_system_policy < OptIn)
|
|
|
|
return;
|
2009-12-19 04:32:04 +08:00
|
|
|
if (!GetProcessDEPPolicy (GetCurrentProcess (), &ppolicy, &perm))
|
2008-11-12 19:04:27 +08:00
|
|
|
{
|
|
|
|
debug_printf ("GetProcessDEPPolicy: %E");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug_printf ("DEP Process Policy: %d (permanent = %d)", ppolicy, perm);
|
|
|
|
if (ppolicy > 0 && !perm && !SetProcessDEPPolicy (0))
|
|
|
|
debug_printf ("SetProcessDEPPolicy: %E");
|
|
|
|
}
|
2009-03-04 19:45:58 +08:00
|
|
|
#endif
|
2008-11-12 19:04:27 +08:00
|
|
|
|
2005-05-09 09:19:38 +08:00
|
|
|
void __stdcall
|
|
|
|
dll_crt0_0 ()
|
|
|
|
{
|
2005-11-06 22:02:33 +08:00
|
|
|
init_global_security ();
|
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
|
|
|
|
2006-03-14 02:29:48 +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. */
|
2008-10-06 00:48:30 +08:00
|
|
|
if (!dynamically_loaded)
|
|
|
|
sigproc_init ();
|
2006-03-14 02:29:48 +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);
|
|
|
|
|
2009-12-19 04:32:04 +08:00
|
|
|
OpenProcessToken (GetCurrentProcess (), 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
|
|
|
|
2005-05-09 09:19:38 +08:00
|
|
|
child_proc_info = get_cygwin_startup_info ();
|
2002-10-15 15:03:45 +08:00
|
|
|
if (!child_proc_info)
|
2009-06-08 11:53:40 +08:00
|
|
|
memory_init (true);
|
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
|
|
|
{
|
2001-10-10 10:32:12 +08:00
|
|
|
case _PROC_FORK:
|
2005-12-30 04:46:34 +08:00
|
|
|
fork_info->handle_fork ();
|
2000-09-03 12:16:35 +08:00
|
|
|
break;
|
2001-10-10 10:32:12 +08:00
|
|
|
case _PROC_SPAWN:
|
|
|
|
case _PROC_EXEC:
|
2006-03-21 02:01:17 +08:00
|
|
|
spawn_info->handle_spawn ();
|
2000-09-03 12:16:35 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-04-06 00:42:27 +08:00
|
|
|
user_data->threadinterface->Init ();
|
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::init ();
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
/* Initialize events */
|
|
|
|
events_init ();
|
2006-06-04 04:32:07 +08:00
|
|
|
tty_list::init_session ();
|
2004-01-14 23:45:37 +08:00
|
|
|
|
2009-03-04 19:45:58 +08:00
|
|
|
#if 0
|
|
|
|
/* Setting the TS-aware flag in the application's PE header is sufficient.
|
|
|
|
Just keep this in as a reminder. */
|
2008-11-12 19:04:27 +08:00
|
|
|
|
2009-03-04 19:45:58 +08:00
|
|
|
/* The disable_dep function disables DEP for all Cygwin processes if
|
2008-11-12 19:04:27 +08:00
|
|
|
the process runs on a Windows Server 2008 with Terminal Services
|
|
|
|
installed. This combination (TS+DEP) breaks *some* Cygwin
|
|
|
|
applications. The Terminal Service specific DLL tsappcmp.dll
|
|
|
|
changes the page protection of some pages in the application's text
|
|
|
|
segment from PAGE_EXECUTE_WRITECOPY to PAGE_WRITECOPY for no
|
|
|
|
apparent reason. This occurs before any Cygwin or applicaton code
|
|
|
|
had a chance to run. MS has no explanation for this so far, but is
|
|
|
|
rather busy trying to avoid giving support for this problem (as of
|
|
|
|
2008-11-11).
|
|
|
|
|
|
|
|
Unfortunately disabling DEP seems to have a not negligible
|
|
|
|
performance hit. In the long run, either MS has to fix their
|
|
|
|
problem, or we have to find a better workaround, if any exists.
|
|
|
|
Idle idea: Adding EXECUTE protection to all text segment pages? */
|
|
|
|
if (wincap.ts_has_dep_problem ())
|
|
|
|
disable_dep ();
|
2009-03-04 19:45:58 +08:00
|
|
|
#endif
|
2008-11-12 19:04:27 +08:00
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
debug_printf ("finished dll_crt0_0 initialization");
|
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 ();
|
|
|
|
|
2008-10-06 00:48:30 +08:00
|
|
|
if (dynamically_loaded)
|
|
|
|
sigproc_init ();
|
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
|
|
|
|
2005-08-08 12:06:07 +08:00
|
|
|
#ifdef CGF
|
|
|
|
int i = 0;
|
2005-08-18 00:10:48 +08:00
|
|
|
const int n = 2 * 1024 * 1024;
|
|
|
|
while (i--)
|
|
|
|
small_printf ("cmalloc returns %p\n", cmalloc (HEAP_STR, n));
|
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
|
|
|
|
|
2006-03-29 14:35:25 +08:00
|
|
|
create_signal_arrived (); /* FIXME: move into wait_sig? */
|
2005-06-16 06:50:31 +08:00
|
|
|
|
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. */
|
2000-09-03 12:16:35 +08:00
|
|
|
if (fork_info->stacksize)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-12-14 15:09:22 +08:00
|
|
|
_tlsbase = (char *) fork_info->stackbottom;
|
|
|
|
_tlstop = (char *) fork_info->stacktop;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2007-02-22 20:34:55 +08:00
|
|
|
|
2004-09-12 11:47:57 +08:00
|
|
|
longjmp (fork_info->jmp, true);
|
2000-02-18 03:38:33 +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);
|
2000-11-15 14:27:48 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
|
2006-03-29 14:35:25 +08:00
|
|
|
old_title = title_buf;
|
2000-02-18 03:38:33 +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
|
|
|
|
2006-03-23 00:42:45 +08:00
|
|
|
uinfo_init (); /* initialize user info */
|
|
|
|
|
|
|
|
wait_for_sigthread ();
|
|
|
|
extern DWORD threadfunc_ix;
|
|
|
|
if (!threadfunc_ix)
|
|
|
|
system_printf ("internal error: couldn't determine location of thread function on stack. Expect signal problems.");
|
|
|
|
|
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
|
|
|
|
2010-01-02 03:15:11 +08:00
|
|
|
/* Set internal locale to the environment settings. */
|
|
|
|
initial_setlocale ();
|
|
|
|
|
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];
|
|
|
|
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
|
|
|
|
2000-10-15 09:37:07 +08:00
|
|
|
/* Set new console title if appropriate. */
|
|
|
|
|
|
|
|
if (display_title && !dynamically_loaded)
|
|
|
|
{
|
|
|
|
char *cp = __progname;
|
|
|
|
if (strip_title_path)
|
|
|
|
for (char *ptr = cp; *ptr && *ptr != ' '; ptr++)
|
|
|
|
if (isdirsep (*ptr))
|
|
|
|
cp = ptr + 1;
|
|
|
|
set_console_title (cp);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
debug_printf ("user_data->main %p", user_data->main);
|
2000-07-15 10:48:11 +08:00
|
|
|
|
2000-07-18 03:18:21 +08:00
|
|
|
if (dynamically_loaded)
|
|
|
|
{
|
|
|
|
set_errno (0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-07-15 10:48:11 +08:00
|
|
|
set_errno (0);
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Flush signals and ensure that signal thread is up and running. Can't
|
|
|
|
do this for noncygwin case since the signal thread is blocked due to
|
|
|
|
LoadLibrary serialization. */
|
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");
|
2000-06-17 03:36:07 +08:00
|
|
|
if (user_data->main)
|
2005-05-11 00:08:46 +08:00
|
|
|
cygwin_exit (user_data->main (__argc, __argv, *user_data->envptr));
|
2008-08-02 03:12:49 +08:00
|
|
|
__asm__ (" \n\
|
|
|
|
.global __cygwin_exit_return \n\
|
|
|
|
__cygwin_exit_return: \n\
|
|
|
|
");
|
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
|
|
|
{
|
2000-07-15 10:48:11 +08:00
|
|
|
main_environ = user_data->envptr;
|
2005-12-30 04:46:34 +08:00
|
|
|
if (in_forkee)
|
2010-02-10 15:25:26 +08:00
|
|
|
{
|
|
|
|
fork_info->alloc_stack ();
|
|
|
|
_main_tls = &_my_tls;
|
|
|
|
}
|
2004-01-27 02:52:02 +08:00
|
|
|
else
|
2010-02-10 15:25:26 +08:00
|
|
|
{
|
|
|
|
_main_tls = &_my_tls;
|
|
|
|
__sinit (_impure_ptr);
|
|
|
|
}
|
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 ()
|
|
|
|
{
|
|
|
|
static char **envp;
|
|
|
|
static int _fmode;
|
|
|
|
|
|
|
|
user_data->magic_biscuit = sizeof (per_process);
|
|
|
|
|
|
|
|
user_data->envptr = &envp;
|
|
|
|
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);
|
2009-07-30 23:48:51 +08:00
|
|
|
/* Now we have run global ctors, register their dtors. */
|
2005-05-12 05:53:54 +08:00
|
|
|
atexit (do_global_dtors);
|
2009-07-30 23:48:51 +08:00
|
|
|
/* At exit, global dtors will run first, so the app can still
|
|
|
|
use shared library functions while terminating; then the
|
|
|
|
DLLs will be destroyed; finally newlib will shut down stdio
|
|
|
|
and terminate itself. */
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-09-23 12:02:07 +08:00
|
|
|
void __stdcall
|
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)
|
|
|
|
{
|
|
|
|
exit_state = ES_EVENTS_TERMINATE;
|
|
|
|
events_terminate ();
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
UINT n = (UINT) status;
|
2002-10-09 13:55:40 +08:00
|
|
|
if (exit_state < ES_THREADTERM)
|
|
|
|
{
|
|
|
|
exit_state = ES_THREADTERM;
|
|
|
|
cygthread::terminate ();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2000-02-18 03:38:33 +08:00
|
|
|
sigproc_printf ("%d == pgrp %d, send SIG{HUP,CONT} to stopped children",
|
|
|
|
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];
|
|
|
|
sigproc_printf ("%d == sid %d, send SIGHUP to children",
|
|
|
|
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-09-14 22:00:07 +08:00
|
|
|
if (exit_state < ES_TITLE)
|
|
|
|
{
|
|
|
|
exit_state = ES_TITLE;
|
|
|
|
/* restore console title */
|
|
|
|
if (old_title && display_title)
|
|
|
|
set_console_title (old_title);
|
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
if (exit_state < ES_TTY_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_TTY_TERMINATE;
|
2006-06-04 04:32:07 +08:00
|
|
|
cygwin_shared->tty.terminate ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
myself.exit (n);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2010-02-02 10:00:01 +08:00
|
|
|
dll *d = dlls.find ((void *) _my_tls.retaddr ());
|
|
|
|
res = d ? __cxa_atexit ((void (*) (void *)) fn, NULL, d) : 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
|
|
|
|
__api_fatal (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2006-10-27 18:41:51 +08:00
|
|
|
int n = __small_sprintf (buf, "%P: *** fatal error - ");
|
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
|
|
|
}
|
|
|
|
|
2001-10-10 07:28:24 +08:00
|
|
|
void
|
2001-12-26 12:53:34 +08:00
|
|
|
multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned 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
|
|
|
{
|
2002-08-27 12:10:54 +08:00
|
|
|
child_proc_info->type = _PROC_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)
|
|
|
|
system_printf ("%s magic number mismatch detected - %p/%p", what, magic_version, version);
|
|
|
|
else
|
2005-01-06 05:40:08 +08:00
|
|
|
api_fatal ("%s mismatch detected - %p/%p.\n\
|
|
|
|
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
|
|
|
|
void __stdcall
|
|
|
|
cygbench (const char *s)
|
|
|
|
{
|
2008-02-26 02:32:23 +08:00
|
|
|
if (GetEnvironmentVariableA ("CYGWIN_BENCH", NULL, 0))
|
2000-10-17 07:55:58 +08:00
|
|
|
small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
|
|
|
|
}
|
|
|
|
#endif
|