2000-02-17 20:38:33 +01:00
|
|
|
/* dcrt0.cc -- essentially the main() for the Cygwin dll
|
|
|
|
|
2006-01-02 14:01:42 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2007-02-22 11:54:47 +01:00
|
|
|
2006, 2007 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <unistd.h>
|
2003-12-23 17:26:31 +01:00
|
|
|
#include <stdio.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "glob.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include <ctype.h>
|
2000-10-17 01:55:58 +02:00
|
|
|
#include <limits.h>
|
2000-12-10 01:45:12 +01:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sigproc.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "pinfo.h"
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#define NEED_VFORK
|
2001-03-05 07:28:25 +01:00
|
|
|
#include "perprocess.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-06-04 03:28:09 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "dtable.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "cygheap.h"
|
2001-12-26 05:53:34 +01:00
|
|
|
#include "child_info_magic.h"
|
2004-11-26 05:15:10 +01:00
|
|
|
#include "cygtls.h"
|
2000-09-07 18:23:51 +02:00
|
|
|
#include "shared_info.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "cygwin_version.h"
|
|
|
|
#include "dll_init.h"
|
2003-03-01 03:02:42 +01:00
|
|
|
#include "sync.h"
|
2004-01-14 16:45:37 +01:00
|
|
|
#include "heap.h"
|
2006-03-14 20:07:36 +01:00
|
|
|
#include "environ.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#define MAX_AT_FILE_LEVEL 10
|
|
|
|
|
2001-03-03 04:56:34 +01:00
|
|
|
#define PREMAIN_LEN (sizeof (user_data->premain) / sizeof (user_data->premain[0]))
|
2000-07-01 05:51:55 +02:00
|
|
|
|
2005-07-05 22:20:48 +02:00
|
|
|
extern "C" void cygwin_exit (int) __attribute__ ((noreturn));
|
2005-05-10 18:08:46 +02:00
|
|
|
|
2003-10-08 23:40:33 +02:00
|
|
|
HANDLE NO_COPY hMainProc = (HANDLE) -1;
|
2002-08-01 18:20:31 +02:00
|
|
|
HANDLE NO_COPY hMainThread;
|
2005-04-03 15:06:43 +02:00
|
|
|
HANDLE NO_COPY hProcToken;
|
|
|
|
HANDLE NO_COPY hProcImpToken;
|
2005-10-24 01:47:45 +02:00
|
|
|
|
|
|
|
muto NO_COPY lock_process::locker;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-07-02 05:16:00 +02:00
|
|
|
bool display_title;
|
|
|
|
bool strip_title_path;
|
2003-12-07 23:37:12 +01:00
|
|
|
bool allow_glob = true;
|
2005-12-29 21:46:34 +01:00
|
|
|
bool NO_COPY in_forkee;
|
2000-12-10 01:45:12 +01:00
|
|
|
codepage_type current_codepage = ansi_cp;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-12-12 06:04:47 +01:00
|
|
|
int __argc_safe;
|
|
|
|
int _declspec(dllexport) __argc;
|
|
|
|
char _declspec(dllexport) **__argv;
|
2004-01-24 00:05:33 +01:00
|
|
|
#ifdef NEWVFORK
|
2004-01-14 16:45:37 +01:00
|
|
|
vfork_save NO_COPY *main_vfork;
|
2004-01-24 00:05:33 +01:00
|
|
|
#endif
|
2004-01-14 16:45:37 +01:00
|
|
|
|
|
|
|
static int NO_COPY envc;
|
|
|
|
char NO_COPY **envp;
|
2003-12-12 06:04:47 +01:00
|
|
|
|
|
|
|
extern "C" void __sinit (_reent *);
|
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls NO_COPY *_main_tls;
|
2003-12-12 06:04:47 +01:00
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
bool NO_COPY cygwin_finished_initializing;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Used in SIGTOMASK for generating a bit for insertion into a sigset_t.
|
|
|
|
This is subtracted from the signal number prior to shifting the bit.
|
|
|
|
In older versions of cygwin, the signal was used as-is to shift the
|
|
|
|
bit for masking. So, we'll temporarily detect this and set it to zero
|
|
|
|
for programs that are linked using older cygwins. This is just a stopgap
|
|
|
|
measure to allow an orderly transfer to the new, correct sigmask method. */
|
2001-09-06 07:17:22 +02:00
|
|
|
unsigned NO_COPY int signal_shift_subtract = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-06 01:46:44 +02:00
|
|
|
ResourceLocks _reslock NO_COPY;
|
2001-09-11 10:15:39 +02:00
|
|
|
MTinterface _mtinterf;
|
2000-07-06 01:46:44 +02:00
|
|
|
|
2001-09-03 04:13:05 +02:00
|
|
|
bool NO_COPY _cygwin_testing;
|
|
|
|
|
2002-06-27 22:44:27 +02:00
|
|
|
char NO_COPY almost_null[1];
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
/* This is an exported copy of environ which can be used by DLLs
|
|
|
|
which use cygwin.dll. */
|
|
|
|
char **__cygwin_environ;
|
2006-03-14 20:07:36 +01:00
|
|
|
char ***main_environ = &__cygwin_environ;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* __progname used in getopt error message */
|
2001-09-06 06:41:59 +02:00
|
|
|
char *__progname;
|
2000-07-06 01:46:44 +02:00
|
|
|
struct per_process __cygwin_user_data =
|
|
|
|
{/* initial_sp */ 0, /* magic_biscuit */ 0,
|
|
|
|
/* dll_major */ CYGWIN_VERSION_DLL_MAJOR,
|
|
|
|
/* dll_major */ CYGWIN_VERSION_DLL_MINOR,
|
2000-07-12 06:02:45 +02:00
|
|
|
/* impure_ptr_ptr */ NULL, /* envptr */ NULL,
|
2002-08-16 21:41:39 +02:00
|
|
|
/* malloc */ malloc, /* free */ free,
|
|
|
|
/* realloc */ realloc,
|
2000-07-06 01:46:44 +02:00
|
|
|
/* fmode_ptr */ NULL, /* main */ NULL, /* ctors */ NULL,
|
|
|
|
/* dtors */ NULL, /* data_start */ NULL, /* data_end */ NULL,
|
|
|
|
/* bss_start */ NULL, /* bss_end */ NULL,
|
2002-08-16 21:41:39 +02:00
|
|
|
/* calloc */ calloc,
|
2000-07-06 01:46:44 +02:00
|
|
|
/* premain */ {NULL, NULL, NULL, NULL},
|
|
|
|
/* run_ctors_p */ 0,
|
2001-09-07 23:32:07 +02:00
|
|
|
/* unused */ {0, 0, 0, 0, 0, 0, 0},
|
2005-12-29 21:46:34 +01:00
|
|
|
/* UNUSED forkee */ 0,
|
2001-09-07 23:32:07 +02:00
|
|
|
/* hmodule */ NULL,
|
2000-07-06 01:46:44 +02:00
|
|
|
/* api_major */ CYGWIN_VERSION_API_MAJOR,
|
|
|
|
/* api_minor */ CYGWIN_VERSION_API_MINOR,
|
|
|
|
/* unused2 */ {0, 0, 0, 0, 0},
|
|
|
|
/* resourcelocks */ &_reslock, /* threadinterface */ &_mtinterf,
|
2004-09-16 05:49:03 +02:00
|
|
|
/* impure_ptr */ _GLOBAL_REENT,
|
2000-07-06 01:46:44 +02:00
|
|
|
};
|
2001-09-06 06:41:59 +02:00
|
|
|
bool ignore_case_with_glob;
|
2003-12-07 23:37:12 +01:00
|
|
|
int __declspec (dllexport) _check_for_executable = true;
|
2001-03-05 07:28:25 +01:00
|
|
|
#ifdef DEBUGGING
|
2001-09-06 06:41:59 +02:00
|
|
|
int pinger;
|
2001-03-05 07:28:25 +01:00
|
|
|
#endif
|
2006-03-13 22:10:14 +01:00
|
|
|
int NO_COPY __api_fatal_exit_val = 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
char *old_title;
|
2000-02-17 20:38:33 +01:00
|
|
|
char title_buf[TITLESIZE + 1];
|
|
|
|
|
|
|
|
static void
|
2005-05-11 23:53:54 +02:00
|
|
|
do_global_dtors ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-04-14 19:34:03 +02:00
|
|
|
void (**pfunc) () = user_data->dtors;
|
|
|
|
if (pfunc)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-04-14 19:34:03 +02:00
|
|
|
user_data->dtors = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
while (*++pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
do_global_ctors (void (**in_pfunc)(), int force)
|
|
|
|
{
|
2005-12-29 21:46:34 +01:00
|
|
|
if (!force && in_forkee)
|
2003-12-23 17:26:31 +01:00
|
|
|
return; // inherit constructed stuff from parent pid
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Run ctors backwards, so skip the first entry and find how many
|
|
|
|
there are, then run them. */
|
|
|
|
|
2003-12-23 17:26:31 +01:00
|
|
|
void (**pfunc) () = in_pfunc;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
while (*++pfunc)
|
|
|
|
;
|
|
|
|
while (--pfunc > in_pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-08-15 17:46:27 +02: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-17 20:38:33 +01:00
|
|
|
*/
|
2003-12-07 23:37:12 +01:00
|
|
|
static bool __stdcall
|
2000-05-07 05:29:08 +02:00
|
|
|
insert_file (char *name, char *&cmd)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
HANDLE f;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
f = CreateFile (name + 1,
|
|
|
|
GENERIC_READ, /* open for reading */
|
|
|
|
FILE_SHARE_READ, /* share for reading */
|
|
|
|
&sec_none_nih, /* no security */
|
|
|
|
OPEN_EXISTING, /* existing file only */
|
|
|
|
FILE_ATTRIBUTE_NORMAL, /* normal file */
|
|
|
|
NULL); /* no attr. template */
|
|
|
|
|
|
|
|
if (f == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
debug_printf ("couldn't open file '%s', %E", name);
|
2003-12-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01: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-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int new_size = strlen (cmd) + size + 2;
|
|
|
|
char *tmp = (char *) malloc (new_size);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
debug_printf ("malloc failed, %E");
|
2003-12-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01: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-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp[size++] = ' ';
|
|
|
|
strcpy (tmp + size, cmd);
|
|
|
|
cmd = tmp;
|
2003-12-07 23:37:12 +01:00
|
|
|
return true;
|
2000-02-17 20:38:33 +01: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 08:31:06 +02:00
|
|
|
static /*__inline*/ char *
|
2000-02-21 06:20:38 +01:00
|
|
|
quoted (char *cmd, int winshell)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char quote = *cmd;
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!winshell)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
char *p;
|
|
|
|
strcpy (cmd, cmd + 1);
|
2003-09-14 03:58:03 +02:00
|
|
|
if (*(p = strechr (cmd, quote)))
|
2000-09-03 06:16:35 +02:00
|
|
|
strcpy (p, p + 1);
|
2000-09-13 21:57:00 +02:00
|
|
|
return p;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-09-26 02:52:21 +02:00
|
|
|
const char *s = quote == '\'' ? "'" : "\\\"";
|
2000-09-03 06:16:35 +02:00
|
|
|
/* This must have been run from a Windows shell, so preserve
|
|
|
|
quotes for globify to play with later. */
|
2000-09-26 02:52:21 +02:00
|
|
|
while (*cmd && *++cmd)
|
|
|
|
if ((p = strpbrk (cmd, s)) == NULL)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
cmd = strchr (cmd, '\0'); // no closing quote
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (*p == '\\')
|
|
|
|
cmd = ++p;
|
2000-09-26 02:52:21 +02:00
|
|
|
else if (quote == '"' && p[1] == '"')
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
*p = '\\';
|
|
|
|
cmd = ++p; // a quoted quote
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd = p + 1; // point to after end
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cmd;
|
2000-02-17 20:38:33 +01: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 02:38:51 +02:00
|
|
|
int dos_spec = isdrive (word);
|
2001-03-03 04:56:34 +01:00
|
|
|
if (!dos_spec && isquote (*word) && word[1] && word[2])
|
2000-05-30 02:38:51 +02:00
|
|
|
dos_spec = isdrive (word + 1);
|
2000-02-17 20:38:33 +01: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 01:16:56 +02:00
|
|
|
if (dos_spec || *s != '\\')
|
2000-04-18 23:28:45 +02:00
|
|
|
/* nothing */;
|
|
|
|
else if (s[1] == quote || s[1] == '\\')
|
2000-02-17 20:38:33 +01:00
|
|
|
s++;
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
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 06:35:18 +02:00
|
|
|
debug_printf ("argv[%d] = '%s'", n++, *gv);
|
2000-02-17 20:38:33 +01: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 06:37:43 +02:00
|
|
|
cmd = quoted (cmd, winshell && argc > 0);
|
2000-02-17 20:38:33 +01: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 05:29:08 +02:00
|
|
|
if (insert_file (word, cmd))
|
2000-02-17 20:38:33 +01: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 06:35:18 +02:00
|
|
|
debug_printf ("argv[%d] = '%s'", argc, word);
|
2000-02-17 20:38:33 +01:00
|
|
|
argv[argc++] = word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv[argc] = NULL;
|
2000-09-03 06:16:35 +02:00
|
|
|
|
|
|
|
debug_printf ("argc %d", argc);
|
2000-02-17 20:38:33 +01: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-11 19:55:29 +01:00
|
|
|
api_fatal ("per_process sanity check failed");
|
2000-02-17 20:38:33 +01: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-22 18:22:59 +01: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-17 20:38:33 +01:00
|
|
|
p->api_major, cygwin_version.api_major);
|
|
|
|
|
|
|
|
if (CYGWIN_VERSION_DLL_MAKE_COMBINED (p->dll_major, p->dll_minor) <=
|
|
|
|
CYGWIN_VERSION_DLL_BAD_SIGNAL_MASK)
|
|
|
|
signal_shift_subtract = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-15 23:59:32 +02:00
|
|
|
child_info NO_COPY *child_proc_info = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2007-02-22 13:34:55 +01:00
|
|
|
#define CYGWIN_GUARD (PAGE_EXECUTE_READWRITE | PAGE_GUARD)
|
2001-07-29 04:33:30 +02:00
|
|
|
|
2006-04-03 19:33:07 +02:00
|
|
|
void
|
|
|
|
child_info_fork::alloc_stack_hard_way (volatile char *b)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
void *new_stack_pointer;
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
2000-09-03 06:16:35 +02:00
|
|
|
void *newbase;
|
|
|
|
int newlen;
|
2007-02-22 13:34:55 +01:00
|
|
|
bool guard;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-04-03 18:04:02 +02: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-03 19:33:07 +02:00
|
|
|
if (stacktop > (LPBYTE) m.AllocationBase && stacktop < curbot)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
newbase = curbot;
|
2006-04-03 19:33:07 +02:00
|
|
|
newlen = (LPBYTE) stackbottom - (LPBYTE) curbot;
|
2007-02-22 13:34:55 +01:00
|
|
|
guard = false;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-22 13:34:55 +01:00
|
|
|
newbase = (LPBYTE) stacktop - (128 * 1024);
|
|
|
|
newlen = (LPBYTE) stackbottom - (LPBYTE) newbase;
|
|
|
|
guard = true;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2007-02-22 13:34:55 +01:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!VirtualAlloc (newbase, newlen, MEM_RESERVE, PAGE_NOACCESS))
|
2000-02-17 20:38:33 +01:00
|
|
|
api_fatal ("fork: can't reserve memory for stack %p - %p, %E",
|
2006-04-03 19:33:07 +02:00
|
|
|
stacktop, stackbottom);
|
2007-02-22 13:34:55 +01:00
|
|
|
new_stack_pointer = (void *) ((LPBYTE) stackbottom - (stacksize += 8192));
|
2006-04-03 19:33:07 +02:00
|
|
|
if (!VirtualAlloc (new_stack_pointer, stacksize, MEM_COMMIT,
|
2000-02-17 20:38:33 +01:00
|
|
|
PAGE_EXECUTE_READWRITE))
|
|
|
|
api_fatal ("fork: can't commit memory for stack %p(%d), %E",
|
2006-04-03 19:33:07 +02:00
|
|
|
new_stack_pointer, stacksize);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!VirtualQuery ((LPCVOID) new_stack_pointer, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get new stack info, %E");
|
2007-02-22 13:34:55 +01:00
|
|
|
|
|
|
|
if (guard)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2007-02-22 13:34:55 +01:00
|
|
|
m.BaseAddress = (LPBYTE) m.BaseAddress - 1;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!VirtualAlloc ((LPVOID) m.BaseAddress, 1, MEM_COMMIT,
|
2002-08-15 22:34:49 +02:00
|
|
|
CYGWIN_GUARD))
|
2000-09-03 06:16:35 +02:00
|
|
|
api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
|
|
|
|
m.BaseAddress);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!VirtualQuery ((LPCVOID) m.BaseAddress, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get new stack info, %E");
|
2006-04-03 19:33:07 +02:00
|
|
|
stacktop = m.BaseAddress;
|
2004-02-14 05:38:37 +01:00
|
|
|
b[0] = '\0';
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2005-09-28 17:18:49 +02:00
|
|
|
void *getstack (void *) __attribute__ ((noinline));
|
|
|
|
volatile char *
|
2005-12-02 04:12:06 +01:00
|
|
|
getstack (volatile char * volatile p)
|
2005-09-28 17:18:49 +02:00
|
|
|
{
|
2005-12-02 04:12:06 +01:00
|
|
|
*p ^= 1;
|
|
|
|
*p ^= 1;
|
2005-09-28 17:18:49 +02:00
|
|
|
return p - 4096;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* extend the stack prior to fork longjmp */
|
|
|
|
|
2006-04-03 19:33:07 +02:00
|
|
|
void
|
|
|
|
child_info_fork::alloc_stack ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-12-02 04:12:06 +01:00
|
|
|
volatile char * volatile esp;
|
2005-09-28 17:18:49 +02:00
|
|
|
__asm__ volatile ("movl %%esp,%0": "=r" (esp));
|
2006-04-03 19:33:07 +02:00
|
|
|
if (_tlsbase != stackbottom)
|
|
|
|
alloc_stack_hard_way (esp);
|
2004-02-14 05:38:37 +01:00
|
|
|
else
|
2005-09-28 17:18:49 +02:00
|
|
|
{
|
2006-04-03 19:33:07 +02:00
|
|
|
char *st = (char *) stacktop - 4096;
|
|
|
|
while (_tlstop >= st)
|
2005-09-28 17:18:49 +02:00
|
|
|
esp = getstack (esp);
|
2006-04-03 19:33:07 +02:00
|
|
|
stacksize = 0;
|
2005-09-28 17:18:49 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2006-06-03 04:58:59 +02:00
|
|
|
extern "C" void
|
2004-01-14 16:45:37 +01:00
|
|
|
break_here ()
|
|
|
|
{
|
2006-06-03 04:58:59 +02:00
|
|
|
static int NO_COPY sent_break;
|
|
|
|
if (!sent_break++)
|
|
|
|
DebugBreak ();
|
2004-01-14 16:45:37 +01:00
|
|
|
debug_printf ("break here");
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
static void
|
2004-02-18 23:32:15 +01:00
|
|
|
initial_env ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-04-03 10:45:21 +02:00
|
|
|
char buf[CYG_MAX_PATH];
|
2004-02-18 23:32:15 +01:00
|
|
|
if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
|
2004-01-20 00:03:43 +01:00
|
|
|
_cygwin_testing = 1;
|
2004-02-18 23:32:15 +01:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
DWORD len;
|
2004-01-20 00:03:43 +01:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
|
|
|
|
{
|
|
|
|
DWORD ms = atoi (buf);
|
|
|
|
buf[0] = '\0';
|
|
|
|
len = GetModuleFileName (NULL, buf, CYG_MAX_PATH);
|
|
|
|
console_printf ("Sleeping %d, pid %u %s\n", ms, GetCurrentProcessId (), buf);
|
2004-01-19 06:46:54 +01:00
|
|
|
Sleep (ms);
|
2005-12-29 21:46:34 +01:00
|
|
|
if (!strace.active () && !dynamically_loaded)
|
|
|
|
strace.hello ();
|
2004-01-14 16:45:37 +01:00
|
|
|
}
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
|
|
|
|
{
|
2005-04-03 10:45:21 +02:00
|
|
|
char buf1[CYG_MAX_PATH];
|
2004-01-14 16:45:37 +01:00
|
|
|
len = GetModuleFileName (NULL, buf1, CYG_MAX_PATH);
|
|
|
|
strlwr (buf1);
|
|
|
|
strlwr (buf);
|
2005-03-10 15:41:50 +01:00
|
|
|
char *p = strpbrk (buf, ":=");
|
2004-01-14 16:45:37 +01:00
|
|
|
if (!p)
|
|
|
|
p = (char *) "gdb.exe -nw";
|
|
|
|
else
|
|
|
|
*p++ = '\0';
|
|
|
|
if (strstr (buf1, buf))
|
|
|
|
{
|
|
|
|
error_start_init (p);
|
|
|
|
try_to_debug ();
|
2004-02-18 23:32:15 +01:00
|
|
|
console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n");
|
2004-01-14 16:45:37 +01:00
|
|
|
break_here ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2005-05-09 03:19:38 +02:00
|
|
|
child_info *
|
|
|
|
get_cygwin_startup_info ()
|
2004-01-14 16:45:37 +01:00
|
|
|
{
|
2005-05-09 03:19:38 +02:00
|
|
|
STARTUPINFO si;
|
2002-06-10 19:08:09 +02:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
GetStartupInfo (&si);
|
2005-05-09 03:19:38 +02:00
|
|
|
child_info *res = (child_info *) si.lpReserved2;
|
2006-12-05 11:59:21 +01:00
|
|
|
|
2005-05-09 03:19:38 +02:00
|
|
|
if (si.cbReserved2 < EXEC_MAGIC_SIZE || !res
|
2006-12-11 19:55:29 +01:00
|
|
|
|| res->intro != PROC_MAGIC_GENERIC || res->magic != CHILD_INFO_MAGIC)
|
2005-05-09 03:19:38 +02:00
|
|
|
res = NULL;
|
2004-01-14 16:45:37 +01:00
|
|
|
else
|
|
|
|
{
|
2005-05-09 03:19:38 +02: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 16:45:37 +01:00
|
|
|
(DWORD) &_cygheap_start);
|
2005-05-09 03:19:38 +02:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
unsigned should_be_cb = 0;
|
2005-05-09 03:19:38 +02:00
|
|
|
switch (res->type)
|
2004-01-14 16:45:37 +01:00
|
|
|
{
|
|
|
|
case _PROC_FORK:
|
2005-12-29 21:46:34 +01:00
|
|
|
in_forkee = true;
|
2004-01-14 16:45:37 +01:00
|
|
|
should_be_cb = sizeof (child_info_fork);
|
|
|
|
/* fall through */;
|
|
|
|
case _PROC_SPAWN:
|
|
|
|
case _PROC_EXEC:
|
|
|
|
if (!should_be_cb)
|
2004-12-05 20:41:26 +01:00
|
|
|
should_be_cb = sizeof (child_info_spawn);
|
2005-05-09 03:19:38 +02: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-20 19:01:17 +01:00
|
|
|
if (res->isstraced ())
|
2005-12-29 21:46:34 +01:00
|
|
|
{
|
|
|
|
res->ready (false);
|
|
|
|
for (unsigned i = 0; !being_debugged () && i < 10000; i++)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
strace.hello ();
|
|
|
|
}
|
2005-01-11 16:31:04 +01:00
|
|
|
break;
|
2004-01-14 16:45:37 +01:00
|
|
|
default:
|
2005-05-09 03:19:38 +02:00
|
|
|
system_printf ("unknown exec type %d", res->type);
|
2004-01-14 16:45:37 +01:00
|
|
|
/* intentionally fall through */
|
|
|
|
case _PROC_WHOOPS:
|
2005-05-09 03:19:38 +02:00
|
|
|
res = NULL;
|
2004-01-14 16:45:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 03:19:38 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-12-17 05:36:39 +01: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-29 21:46:34 +01:00
|
|
|
child_info_fork::handle_fork ()
|
2005-12-17 05:36:39 +01:00
|
|
|
{
|
|
|
|
cygheap_fixup_in_child (false);
|
|
|
|
memory_init ();
|
|
|
|
set_myself (NULL);
|
2006-03-29 08:35:25 +02:00
|
|
|
myself->uid = cygheap->user.real_uid;
|
|
|
|
myself->gid = cygheap->user.real_gid;
|
|
|
|
|
2005-12-29 21:46:34 +01:00
|
|
|
child_copy (parent, false,
|
2005-12-17 05:36:39 +01: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);
|
|
|
|
/* 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-29 21:46:34 +01:00
|
|
|
child_copy (parent, false,
|
2005-12-17 05:36:39 +01:00
|
|
|
"data", user_data->data_start, user_data->data_end,
|
|
|
|
"bss", user_data->bss_start, user_data->bss_end,
|
|
|
|
NULL);
|
|
|
|
|
2005-12-29 21:46:34 +01:00
|
|
|
if (fixup_mmaps_after_fork (parent))
|
2005-12-17 05:36:39 +01:00
|
|
|
api_fatal ("recreate_mmaps_after_fork_failed");
|
|
|
|
}
|
|
|
|
|
2006-03-20 19:01:17 +01:00
|
|
|
void
|
|
|
|
child_info_spawn::handle_spawn ()
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
cygheap_fixup_in_child (true);
|
|
|
|
memory_init ();
|
|
|
|
if (!moreinfo->myself_pinfo ||
|
|
|
|
!DuplicateHandle (hMainProc, moreinfo->myself_pinfo, hMainProc, &h, 0,
|
|
|
|
FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
|
|
|
h = NULL;
|
|
|
|
set_myself (h);
|
|
|
|
__argc = moreinfo->argc;
|
|
|
|
__argv = moreinfo->argv;
|
|
|
|
envp = moreinfo->envp;
|
|
|
|
envc = moreinfo->envc;
|
|
|
|
if (!dynamically_loaded)
|
|
|
|
cygheap->fdtab.fixup_after_exec ();
|
2006-12-11 19:55:29 +01:00
|
|
|
if (__stdin >= 0)
|
|
|
|
cygheap->fdtab.move_fd (__stdin, 0);
|
|
|
|
if (__stdout >= 0)
|
|
|
|
cygheap->fdtab.move_fd (__stdout, 1);
|
|
|
|
|
2006-06-03 08:35:10 +02:00
|
|
|
ready (true);
|
2006-06-02 02:09:50 +02: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-20 19:01:17 +01:00
|
|
|
signal_fixup_after_exec ();
|
|
|
|
if (moreinfo->old_title)
|
|
|
|
{
|
|
|
|
old_title = strcpy (title_buf, moreinfo->old_title);
|
|
|
|
cfree (moreinfo->old_title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 03:19:38 +02:00
|
|
|
void __stdcall
|
|
|
|
dll_crt0_0 ()
|
|
|
|
{
|
2005-11-06 15:02:33 +01:00
|
|
|
init_global_security ();
|
2006-03-13 19:29:48 +01:00
|
|
|
initial_env ();
|
|
|
|
|
2006-03-21 02:37:25 +01:00
|
|
|
SetErrorMode (SEM_FAILCRITICALERRORS);
|
|
|
|
|
2006-03-13 19:29:48 +01: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. */
|
|
|
|
sigproc_init ();
|
|
|
|
|
2005-10-24 01:47:45 +02:00
|
|
|
lock_process::init ();
|
2005-09-28 17:18:49 +02: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-29 21:46:34 +01:00
|
|
|
user_data->impure_ptr = _impure_ptr;
|
|
|
|
user_data->impure_ptr_ptr = &_impure_ptr;
|
2005-05-09 03:19:38 +02:00
|
|
|
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
|
|
|
|
GetCurrentProcess (), &hMainProc, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
hMainProc = GetCurrentProcess ();
|
|
|
|
|
|
|
|
DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
|
|
|
|
&hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
|
2007-02-23 16:15:50 +01:00
|
|
|
OpenProcessToken (hMainProc, MAXIMUM_ALLOWED, &hProcToken);
|
2005-05-09 03:19:38 +02:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
device::init ();
|
|
|
|
do_global_ctors (&__CTOR_LIST__, 1);
|
|
|
|
cygthread::init ();
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2005-05-09 03:19:38 +02:00
|
|
|
child_proc_info = get_cygwin_startup_info ();
|
2002-10-15 09:03:45 +02:00
|
|
|
if (!child_proc_info)
|
2006-03-20 02:11:30 +01:00
|
|
|
memory_init ();
|
2002-10-15 09:03:45 +02:00
|
|
|
else
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2005-05-09 03:19:38 +02:00
|
|
|
cygwin_user_h = child_proc_info->user_h;
|
2001-12-26 05:53:34 +01:00
|
|
|
switch (child_proc_info->type)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_FORK:
|
2005-12-29 21:46:34 +01:00
|
|
|
fork_info->handle_fork ();
|
2000-09-03 06:16:35 +02:00
|
|
|
break;
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_SPAWN:
|
|
|
|
case _PROC_EXEC:
|
2006-03-20 19:01:17 +01:00
|
|
|
spawn_info->handle_spawn ();
|
2000-09-03 06:16:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-04-05 18:42:27 +02:00
|
|
|
user_data->resourcelocks->Init ();
|
|
|
|
user_data->threadinterface->Init ();
|
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
_cygtls::init ();
|
2004-01-14 16:45:37 +01:00
|
|
|
|
|
|
|
/* Initialize events */
|
|
|
|
events_init ();
|
2006-06-03 22:32:07 +02:00
|
|
|
tty_list::init_session ();
|
2004-01-14 16:45:37 +01:00
|
|
|
|
|
|
|
cygheap->cwd.init ();
|
2005-04-03 15:06:43 +02:00
|
|
|
|
2005-12-29 21:46:34 +01:00
|
|
|
debug_printf ("finished dll_crt0_0 initialization");
|
2004-01-14 16:45:37 +01: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 02:09:50 +02:00
|
|
|
void
|
|
|
|
dll_crt0_1 (void *)
|
2004-01-14 16:45:37 +01:00
|
|
|
{
|
|
|
|
check_sanity_and_sync (user_data);
|
|
|
|
malloc_init ();
|
2005-08-08 06:06:07 +02:00
|
|
|
#ifdef CGF
|
|
|
|
int i = 0;
|
2005-08-17 18:10:48 +02:00
|
|
|
const int n = 2 * 1024 * 1024;
|
|
|
|
while (i--)
|
|
|
|
small_printf ("cmalloc returns %p\n", cmalloc (HEAP_STR, n));
|
2005-08-08 06:06:07 +02:00
|
|
|
#endif
|
2004-01-14 16:45:37 +01:00
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
ProtectHandle (hMainProc);
|
|
|
|
ProtectHandle (hMainThread);
|
|
|
|
|
2003-11-28 21:55:59 +01:00
|
|
|
/* Initialize pthread mainthread when not forked and it is safe to call new,
|
2003-05-15 21:42:51 +02:00
|
|
|
otherwise it is reinitalized in fixup_after_fork */
|
2005-12-29 21:46:34 +01:00
|
|
|
if (!in_forkee)
|
2004-01-26 19:52:02 +01:00
|
|
|
pthread::init_mainthread ();
|
2002-11-24 14:54:14 +01:00
|
|
|
|
2003-07-30 06:46:07 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
strace.microseconds ();
|
|
|
|
#endif
|
|
|
|
|
2006-03-29 08:35:25 +02:00
|
|
|
create_signal_arrived (); /* FIXME: move into wait_sig? */
|
2005-06-16 00:50:31 +02:00
|
|
|
|
2002-07-14 21:15:32 +02: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 00:05:33 +01:00
|
|
|
#ifdef NEWVFORK
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab.vfork_child_fixup ();
|
2001-09-15 02:47:44 +02:00
|
|
|
main_vfork = vfork_storage.create ();
|
2004-01-24 00:05:33 +01:00
|
|
|
#endif
|
2000-10-19 02:45:39 +02:00
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
cygbench ("pre-forkee");
|
2005-12-29 21:46:34 +01:00
|
|
|
if (in_forkee)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
/* If we've played with the stack, stacksize != 0. That means that
|
|
|
|
fork() was invoked from other than the main thread. Make sure that
|
|
|
|
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 06:16:35 +02:00
|
|
|
if (fork_info->stacksize)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-12-14 08:09:22 +01:00
|
|
|
_tlsbase = (char *) fork_info->stackbottom;
|
|
|
|
_tlstop = (char *) fork_info->stacktop;
|
2007-02-22 13:34:55 +01:00
|
|
|
_my_tls.init_exception_handler (_cygtls::handle_exceptions);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2007-02-22 13:34:55 +01:00
|
|
|
|
2004-09-12 05:47:57 +02:00
|
|
|
longjmp (fork_info->jmp, true);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-11-15 07:27:48 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
{
|
|
|
|
extern void fork_init ();
|
|
|
|
fork_init ();
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-22 17:42:45 +01:00
|
|
|
pinfo_init (envp, envc);
|
2000-11-15 07:27:48 +01:00
|
|
|
|
2005-04-03 15:06:43 +02:00
|
|
|
/* Can be set only after environment has been initialized. */
|
2007-02-23 16:15:50 +01:00
|
|
|
set_cygwin_privileges (hProcToken);
|
2005-04-03 15:06:43 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
|
2006-03-29 08:35:25 +02:00
|
|
|
old_title = title_buf;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-18 23:10:15 +02:00
|
|
|
/* Allocate cygheap->fdtab */
|
2000-08-12 07:35:42 +02:00
|
|
|
dtable_init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-03-22 17:42:45 +01: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-17 20:38:33 +01:00
|
|
|
/* Connect to tty. */
|
2006-06-03 22:32:07 +02:00
|
|
|
tty::init_session ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!__argc)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-17 01:55:58 +02:00
|
|
|
char *line = GetCommandLineA ();
|
|
|
|
line = strcpy ((char *) alloca (strlen (line) + 1), line);
|
|
|
|
|
2000-12-10 01:45:12 +01:00
|
|
|
if (current_codepage == oem_cp)
|
2001-01-08 05:02:02 +01:00
|
|
|
CharToOemA (line, line);
|
2000-12-10 01:45:12 +01:00
|
|
|
|
2000-09-03 06:16:35 +02: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], '\\')))
|
|
|
|
{
|
2003-12-26 05:40:52 +01:00
|
|
|
char *new_argv0 = (char *) malloc (CYG_MAX_PATH);
|
2000-09-03 06:16:35 +02:00
|
|
|
cygwin_conv_to_posix_path (__argv[0], new_argv0);
|
2003-12-26 05:40:52 +01:00
|
|
|
__argv[0] = (char *) realloc (new_argv0, strlen (new_argv0) + 1);
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2003-09-09 05:11:31 +02:00
|
|
|
__argc_safe = __argc;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (user_data->premain[0])
|
|
|
|
for (unsigned int i = 0; i < PREMAIN_LEN / 2; i++)
|
2001-03-05 07:28:25 +01:00
|
|
|
user_data->premain[i] (__argc, __argv, user_data);
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2001-03-28 05:42:58 +02:00
|
|
|
/* Set up standard fds in file descriptor table. */
|
2002-02-22 20:33:41 +01:00
|
|
|
cygheap->fdtab.stdio_init ();
|
2001-03-28 05:42:58 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Set up __progname for getopt error call. */
|
2002-06-27 18:01:38 +02:00
|
|
|
if (__argv[0] && (__progname = strrchr (__argv[0], '/')))
|
|
|
|
++__progname;
|
|
|
|
else
|
|
|
|
__progname = __argv[0];
|
|
|
|
if (__progname)
|
|
|
|
{
|
|
|
|
char *cp = strchr (__progname, '\0') - 4;
|
|
|
|
if (cp > __progname && strcasematch (cp, ".exe"))
|
|
|
|
*cp = '\0';
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-15 03:37:07 +02: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);
|
|
|
|
}
|
|
|
|
|
2004-02-12 04:01:58 +01:00
|
|
|
cygwin_finished_initializing = true;
|
2000-07-15 04:48:11 +02:00
|
|
|
/* Call init of loaded dlls. */
|
|
|
|
dlls.init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-01 05:51:55 +02:00
|
|
|
/* Execute any specified "premain" functions */
|
2000-07-06 01:46:44 +02:00
|
|
|
if (user_data->premain[PREMAIN_LEN / 2])
|
2000-07-01 05:51:55 +02:00
|
|
|
for (unsigned int i = PREMAIN_LEN / 2; i < PREMAIN_LEN; i++)
|
2001-03-05 07:28:25 +01:00
|
|
|
user_data->premain[i] (__argc, __argv, user_data);
|
2000-07-01 05:51:55 +02:00
|
|
|
|
|
|
|
debug_printf ("user_data->main %p", user_data->main);
|
2000-07-15 04:48:11 +02:00
|
|
|
|
2000-07-17 21:18:21 +02:00
|
|
|
if (dynamically_loaded)
|
|
|
|
{
|
|
|
|
set_errno (0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-11-11 06:36:34 +01:00
|
|
|
/* Disable case-insensitive globbing */
|
2003-12-07 23:37:12 +01:00
|
|
|
ignore_case_with_glob = false;
|
2000-11-11 06:36:34 +01:00
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
set_errno (0);
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
MALLOC_CHECK;
|
2000-10-17 01:55:58 +02:00
|
|
|
cygbench (__progname);
|
2003-12-23 17:26:31 +01: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-22 20:00:31 +01:00
|
|
|
ld_preload ();
|
2000-06-16 21:36:07 +02:00
|
|
|
if (user_data->main)
|
2005-05-10 18:08:46 +02:00
|
|
|
cygwin_exit (user_data->main (__argc, __argv, *user_data->envptr));
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-01 06:26:45 +02:00
|
|
|
extern "C" void __stdcall
|
|
|
|
_dll_crt0 ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-15 04:48:11 +02:00
|
|
|
main_environ = user_data->envptr;
|
2005-12-29 21:46:34 +01:00
|
|
|
if (in_forkee)
|
2006-04-03 19:33:07 +02:00
|
|
|
fork_info->alloc_stack ();
|
2004-01-26 19:52:02 +01:00
|
|
|
else
|
|
|
|
__sinit (_impure_ptr);
|
|
|
|
|
2006-05-25 04:33:13 +02:00
|
|
|
_main_tls = &_my_tls;
|
|
|
|
_main_tls->call ((DWORD (*) (void *, void *)) dll_crt0_1, NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-07-01 05:51:55 +02:00
|
|
|
void
|
|
|
|
dll_crt0 (per_process *uptr)
|
|
|
|
{
|
|
|
|
/* Set the local copy of the pointer into the user space. */
|
2005-12-29 21:46:34 +01:00
|
|
|
if (!in_forkee && uptr && uptr != user_data)
|
2000-07-06 01:46:44 +02:00
|
|
|
{
|
|
|
|
memcpy (user_data, uptr, per_process_overwrite);
|
2004-09-16 05:49:03 +02:00
|
|
|
*(user_data->impure_ptr_ptr) = _GLOBAL_REENT;
|
2000-07-06 01:46:44 +02:00
|
|
|
}
|
2000-07-01 06:26:45 +02:00
|
|
|
_dll_crt0 ();
|
2000-07-01 05:51:55 +02:00
|
|
|
}
|
|
|
|
|
2005-06-04 04:11:50 +02:00
|
|
|
/* This must be called by anyone who uses LoadLibrary to load cygwin1.dll.
|
2005-07-28 19:03:34 +02: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 04:39:13 +02:00
|
|
|
|
2005-07-28 19:03:34 +02:00
|
|
|
See winsup/testsuite/cygload for an example of how to use cygwin1.dll
|
|
|
|
from MSVC and non-cygwin MinGW applications. */
|
2000-07-06 01:46:44 +02:00
|
|
|
extern "C" void
|
2000-06-08 02:55:27 +02: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 16:48:29 +01:00
|
|
|
_dll_crt0 ();
|
2000-06-08 02:55:27 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" void
|
|
|
|
__main (void)
|
|
|
|
{
|
2003-12-07 23:37:12 +01:00
|
|
|
do_global_ctors (user_data->ctors, false);
|
2005-05-11 23:53:54 +02:00
|
|
|
atexit (do_global_dtors);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
exit_states NO_COPY exit_state;
|
|
|
|
|
2003-09-23 06:02:07 +02:00
|
|
|
void __stdcall
|
2000-02-17 20:38:33 +01:00
|
|
|
do_exit (int status)
|
|
|
|
{
|
2003-12-10 04:19:19 +01:00
|
|
|
syscall_printf ("do_exit (%d), exit_state %d", status, exit_state);
|
|
|
|
|
2004-01-24 00:05:33 +01:00
|
|
|
#ifdef NEWVFORK
|
2003-12-10 04:19:19 +01: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 00:05:33 +01:00
|
|
|
#endif
|
2003-12-10 04:19:19 +01:00
|
|
|
|
2005-10-18 01:27:00 +02:00
|
|
|
lock_process until_exit (true);
|
2005-06-07 20:41:31 +02:00
|
|
|
|
|
|
|
if (exit_state < ES_GLOBAL_DTORS)
|
|
|
|
{
|
|
|
|
exit_state = ES_GLOBAL_DTORS;
|
|
|
|
dll_global_dtors ();
|
|
|
|
}
|
2005-04-14 19:34:03 +02:00
|
|
|
|
2003-09-23 06:02:07 +02:00
|
|
|
if (exit_state < ES_EVENTS_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_EVENTS_TERMINATE;
|
|
|
|
events_terminate ();
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
UINT n = (UINT) status;
|
2002-10-09 07:55:40 +02:00
|
|
|
if (exit_state < ES_THREADTERM)
|
|
|
|
{
|
|
|
|
exit_state = ES_THREADTERM;
|
|
|
|
cygthread::terminate ();
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
if (exit_state < ES_SIGNAL)
|
|
|
|
{
|
|
|
|
exit_state = ES_SIGNAL;
|
2004-11-26 05:15:10 +01:00
|
|
|
signal (SIGCHLD, SIG_IGN);
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
signal (SIGQUIT, SIG_IGN);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
if (exit_state < ES_CLOSEALL)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
exit_state = ES_CLOSEALL;
|
2005-06-02 04:36:50 +02:00
|
|
|
close_all_files ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
myself->stopsig = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
if (exit_state < ES_HUP_PGRP)
|
|
|
|
{
|
|
|
|
exit_state = ES_HUP_PGRP;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Kill orphaned children on group leader exit */
|
2000-10-25 05:54:50 +02:00
|
|
|
if (myself->has_pgid_children && myself->pid == myself->pgid)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2006-02-06 19:24:11 +01:00
|
|
|
siginfo_t si = {0};
|
2004-01-19 06:46:54 +01:00
|
|
|
si.si_signo = -SIGHUP;
|
|
|
|
si.si_code = SI_KERNEL;
|
2000-02-17 20:38:33 +01:00
|
|
|
sigproc_printf ("%d == pgrp %d, send SIG{HUP,CONT} to stopped children",
|
|
|
|
myself->pid, myself->pgid);
|
2004-01-19 06:46:54 +01:00
|
|
|
kill_pgrp (myself->pgid, si);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2002-10-13 20:16:33 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
if (exit_state < ES_HUP_SID)
|
|
|
|
{
|
|
|
|
exit_state = ES_HUP_SID;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Kill the foreground process group on session leader exit */
|
2001-04-29 01:48:28 +02:00
|
|
|
if (getpgrp () > 0 && myself->pid == myself->sid && real_tty_attached (myself))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
tty *tp = cygwin_shared->tty[myself->ctty];
|
|
|
|
sigproc_printf ("%d == sid %d, send SIGHUP to children",
|
|
|
|
myself->pid, myself->sid);
|
|
|
|
|
2000-10-25 16:18:56 +02:00
|
|
|
/* CGF FIXME: This can't be right. */
|
2000-02-17 20:38:33 +01:00
|
|
|
if (tp->getsid () == myself->sid)
|
2002-02-22 20:33:41 +01:00
|
|
|
tp->kill_pgrp (SIGHUP);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-10-12 06:38:29 +02:00
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
}
|
|
|
|
|
2005-09-14 16:00:07 +02: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-13 20:16:33 +02:00
|
|
|
if (exit_state < ES_TTY_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_TTY_TERMINATE;
|
2006-06-03 22:32:07 +02:00
|
|
|
cygwin_shared->tty.terminate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2005-01-11 16:31:04 +01:00
|
|
|
myself.exit (n);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2005-04-05 06:31:00 +02:00
|
|
|
static NO_COPY muto atexit_lock;
|
2003-03-01 03:02:42 +01:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
cygwin_atexit (void (*function)(void))
|
|
|
|
{
|
|
|
|
int res;
|
2005-04-05 06:31:00 +02:00
|
|
|
atexit_lock.init ("atexit_lock");
|
|
|
|
atexit_lock.acquire ();
|
2003-03-01 03:02:42 +01:00
|
|
|
res = atexit (function);
|
2005-04-05 06:31:00 +02:00
|
|
|
atexit_lock.release ();
|
2003-03-01 03:02:42 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
cygwin_exit (int n)
|
|
|
|
{
|
2005-05-11 23:53:54 +02:00
|
|
|
dll_global_dtors ();
|
2003-03-01 03:02:42 +01:00
|
|
|
if (atexit_lock)
|
2005-04-05 06:31:00 +02:00
|
|
|
atexit_lock.acquire ();
|
2003-03-01 03:02:42 +01:00
|
|
|
exit (n);
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" void
|
|
|
|
_exit (int n)
|
|
|
|
{
|
2004-11-26 05:15:10 +01:00
|
|
|
do_exit (((DWORD) n & 0xff) << 8);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
__api_fatal (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2006-10-27 12:41:51 +02:00
|
|
|
int n = __small_sprintf (buf, "%P: *** fatal error - ");
|
2004-01-14 16:45:37 +01:00
|
|
|
__small_vsprintf (buf + n, fmt, ap);
|
2000-02-17 20:38:33 +01:00
|
|
|
va_end (ap);
|
2005-12-26 20:34:59 +01:00
|
|
|
strace.prntf (_STRACE_SYSTEM, NULL, "%s", buf);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
2005-07-06 22:05:03 +02:00
|
|
|
try_to_debug ();
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
2006-03-13 22:10:14 +01:00
|
|
|
myself.exit (__api_fatal_exit_val);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-10-10 01:28:24 +02:00
|
|
|
void
|
2001-12-26 05:53:34 +01:00
|
|
|
multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned version)
|
2001-10-10 01:28:24 +02:00
|
|
|
{
|
2002-08-27 06:10:54 +02:00
|
|
|
if (_cygwin_testing && (strstr (what, "proc") || strstr (what, "cygheap")))
|
2001-12-26 22:35:16 +01:00
|
|
|
{
|
2002-08-27 06:10:54 +02:00
|
|
|
child_proc_info->type = _PROC_WHOOPS;
|
2001-12-26 22:35:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2002-01-13 17:55:32 +01:00
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_MISMATCH_OK", buf, sizeof (buf)))
|
|
|
|
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-05 22:40:08 +01: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 01:28:24 +02:00
|
|
|
Search for cygwin1.dll using the Windows Start->Find/Search facility\n\
|
2001-12-26 05:53:34 +01: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-05 22:40:08 +01: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 01:28:24 +02:00
|
|
|
}
|
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
void __stdcall
|
|
|
|
cygbench (const char *s)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_BENCH", buf, sizeof (buf)))
|
|
|
|
small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
|
|
|
|
}
|
|
|
|
#endif
|