2000-02-18 03:38:33 +08:00
|
|
|
/* dcrt0.cc -- essentially the main() for the Cygwin dll
|
|
|
|
|
2004-01-04 02:53:49 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 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"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
2003-12-24 00:26:31 +08:00
|
|
|
#include <stdio.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "glob.h"
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include <ctype.h>
|
2000-10-17 07:55:58 +08:00
|
|
|
#include <limits.h>
|
2000-12-10 08:45:12 +08:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.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-07-27 03:22:24 +08:00
|
|
|
#include "security.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"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "perthread.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"
|
2002-08-02 00:20:31 +08:00
|
|
|
#include "cygthread.h"
|
2003-03-01 10:02:42 +08:00
|
|
|
#include "sync.h"
|
2004-01-14 23:45:37 +08:00
|
|
|
#include "heap.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
|
|
|
|
2003-10-09 05:40:33 +08:00
|
|
|
HANDLE NO_COPY hMainProc = (HANDLE) -1;
|
2002-08-02 00:20:31 +08:00
|
|
|
HANDLE NO_COPY hMainThread;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2000-02-18 03:38:33 +08:00
|
|
|
per_thread_vfork NO_COPY vfork_storage;
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-03-12 11:09:28 +08:00
|
|
|
per_thread NO_COPY *threadstuff[] = {
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2000-02-18 03:38:33 +08:00
|
|
|
&vfork_storage,
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
NULL};
|
|
|
|
|
2003-07-02 11:16:00 +08:00
|
|
|
bool display_title;
|
|
|
|
bool strip_title_path;
|
2003-12-08 06:37:12 +08:00
|
|
|
bool allow_glob = true;
|
2000-12-10 08:45:12 +08:00
|
|
|
codepage_type current_codepage = ansi_cp;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-12-12 13:04:47 +08:00
|
|
|
int __argc_safe;
|
|
|
|
int _declspec(dllexport) __argc;
|
|
|
|
char _declspec(dllexport) **__argv;
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2004-01-14 23:45:37 +08:00
|
|
|
vfork_save NO_COPY *main_vfork;
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
static int NO_COPY envc;
|
|
|
|
char NO_COPY **envp;
|
2003-12-12 13:04:47 +08:00
|
|
|
|
|
|
|
extern "C" void __sinit (_reent *);
|
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls NO_COPY *_main_tls;
|
2003-12-12 13:04:47 +08:00
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
bool NO_COPY cygwin_finished_initializing;
|
2000-02-18 03:38:33 +08: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 13:17:22 +08:00
|
|
|
unsigned NO_COPY int signal_shift_subtract = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-06 07:46:44 +08:00
|
|
|
ResourceLocks _reslock NO_COPY;
|
2001-09-11 16:15:39 +08:00
|
|
|
MTinterface _mtinterf;
|
2000-07-06 07:46:44 +08:00
|
|
|
|
2001-09-03 10:13:05 +08:00
|
|
|
bool NO_COPY _cygwin_testing;
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
char NO_COPY almost_null[1];
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
/* This is an exported copy of environ which can be used by DLLs
|
|
|
|
which use cygwin.dll. */
|
|
|
|
char **__cygwin_environ;
|
2000-07-15 10:48:11 +08:00
|
|
|
char ***main_environ;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* __progname used in getopt error message */
|
2001-09-06 12:41:59 +08:00
|
|
|
char *__progname;
|
2000-07-06 07:46:44 +08: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 12:02:45 +08:00
|
|
|
/* impure_ptr_ptr */ NULL, /* envptr */ NULL,
|
2002-08-17 03:41:39 +08:00
|
|
|
/* malloc */ malloc, /* free */ free,
|
|
|
|
/* realloc */ realloc,
|
2000-07-06 07:46:44 +08:00
|
|
|
/* fmode_ptr */ NULL, /* main */ NULL, /* ctors */ NULL,
|
|
|
|
/* dtors */ NULL, /* data_start */ NULL, /* data_end */ NULL,
|
|
|
|
/* bss_start */ NULL, /* bss_end */ NULL,
|
2002-08-17 03:41:39 +08:00
|
|
|
/* calloc */ calloc,
|
2000-07-06 07:46:44 +08:00
|
|
|
/* premain */ {NULL, NULL, NULL, NULL},
|
|
|
|
/* run_ctors_p */ 0,
|
2001-09-08 05:32:07 +08:00
|
|
|
/* unused */ {0, 0, 0, 0, 0, 0, 0},
|
|
|
|
/* forkee */ 0,
|
|
|
|
/* hmodule */ NULL,
|
2000-07-06 07:46:44 +08: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 11:49:03 +08:00
|
|
|
/* impure_ptr */ _GLOBAL_REENT,
|
2000-07-06 07:46:44 +08:00
|
|
|
};
|
2001-09-06 12:41:59 +08:00
|
|
|
bool ignore_case_with_glob;
|
2003-12-08 06:37:12 +08:00
|
|
|
int __declspec (dllexport) _check_for_executable = true;
|
2001-03-05 14:28:25 +08:00
|
|
|
#ifdef DEBUGGING
|
2001-09-06 12:41:59 +08:00
|
|
|
int pinger;
|
2001-03-05 14:28:25 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-09-06 12:41:59 +08:00
|
|
|
char *old_title;
|
2000-02-18 03:38:33 +08:00
|
|
|
char title_buf[TITLESIZE + 1];
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_global_dtors (void)
|
|
|
|
{
|
|
|
|
if (user_data->dtors)
|
|
|
|
{
|
|
|
|
void (**pfunc)() = user_data->dtors;
|
|
|
|
while (*++pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
do_global_ctors (void (**in_pfunc)(), int force)
|
|
|
|
{
|
2003-12-24 00:26:31 +08:00
|
|
|
if (!force && user_data->forkee)
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-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++ = '\\';
|
|
|
|
*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 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)
|
|
|
|
{
|
|
|
|
api_fatal ("per_process sanity check failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
if (CYGWIN_VERSION_DLL_MAKE_COMBINED (p->dll_major, p->dll_minor) <=
|
|
|
|
CYGWIN_VERSION_DLL_BAD_SIGNAL_MASK)
|
|
|
|
signal_shift_subtract = 0;
|
|
|
|
}
|
|
|
|
|
2002-06-16 05:59:32 +08:00
|
|
|
child_info NO_COPY *child_proc_info = NULL;
|
2002-06-26 12:21:01 +08:00
|
|
|
static MEMORY_BASIC_INFORMATION NO_COPY sm;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-16 04:34:49 +08:00
|
|
|
#define CYGWIN_GUARD ((wincap.has_page_guard ()) ? \
|
2002-09-30 23:17:44 +08:00
|
|
|
PAGE_EXECUTE_READWRITE|PAGE_GUARD : PAGE_NOACCESS)
|
2001-07-29 10:33:30 +08:00
|
|
|
|
2004-02-14 12:38:37 +08:00
|
|
|
static void
|
|
|
|
alloc_stack_hard_way (child_info_fork *ci, 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;
|
|
|
|
LPBYTE curbot = (LPBYTE) sm.BaseAddress + sm.RegionSize;
|
|
|
|
bool noguard;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (ci->stacktop > (LPBYTE) sm.AllocationBase && ci->stacktop < curbot)
|
|
|
|
{
|
|
|
|
newbase = curbot;
|
|
|
|
newlen = (LPBYTE) ci->stackbottom - (LPBYTE) curbot;
|
|
|
|
noguard = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newbase = ci->stacktop;
|
|
|
|
newlen = (DWORD) ci->stackbottom - (DWORD) ci->stacktop;
|
|
|
|
noguard = 0;
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
ci->stacktop, ci->stackbottom);
|
|
|
|
|
|
|
|
new_stack_pointer = (void *) ((LPBYTE) ci->stackbottom - ci->stacksize);
|
|
|
|
|
|
|
|
if (!VirtualAlloc (new_stack_pointer, ci->stacksize, MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE))
|
|
|
|
api_fatal ("fork: can't commit memory for stack %p(%d), %E",
|
|
|
|
new_stack_pointer, ci->stacksize);
|
|
|
|
if (!VirtualQuery ((LPCVOID) new_stack_pointer, &m, sizeof m))
|
|
|
|
api_fatal ("fork: couldn't get new stack info, %E");
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!noguard)
|
|
|
|
{
|
2004-02-14 12:38:37 +08:00
|
|
|
m.BaseAddress = (LPVOID) ((DWORD) 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");
|
|
|
|
ci->stacktop = m.BaseAddress;
|
2004-02-14 12:38:37 +08:00
|
|
|
b[0] = '\0';
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* extend the stack prior to fork longjmp */
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
static void
|
2000-02-18 03:38:33 +08:00
|
|
|
alloc_stack (child_info_fork *ci)
|
|
|
|
{
|
2004-02-14 12:38:37 +08:00
|
|
|
/* FIXME: adding 16384 seems to avoid a stack copy problem during
|
|
|
|
fork on Win95, but I don't know exactly why yet. DJ */
|
|
|
|
volatile char b[ci->stacksize + 16384];
|
|
|
|
|
|
|
|
if (!VirtualQuery ((LPCVOID) &b, &sm, sizeof sm))
|
2001-10-10 10:32:12 +08:00
|
|
|
api_fatal ("fork: couldn't get stack info, %E");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-12-12 13:04:47 +08:00
|
|
|
if (sm.AllocationBase == ci->stacktop)
|
2004-02-14 12:38:37 +08:00
|
|
|
ci->stacksize = 0;
|
|
|
|
else
|
|
|
|
alloc_stack_hard_way (ci, b + sizeof (b) - 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
void
|
|
|
|
break_here ()
|
|
|
|
{
|
|
|
|
debug_printf ("break here");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2004-01-14 23:45:37 +08:00
|
|
|
char buf[CYG_MAX_PATH + 1];
|
2004-02-19 06:32:15 +08:00
|
|
|
if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
|
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
|
|
|
|
DWORD len;
|
2004-01-20 07:03:43 +08:00
|
|
|
|
2004-01-14 23:45:37 +08: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 13:46:54 +08:00
|
|
|
Sleep (ms);
|
|
|
|
if (!strace.active)
|
|
|
|
{
|
|
|
|
strace.inited = 0;
|
|
|
|
strace.hello ();
|
|
|
|
}
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
|
|
|
|
{
|
|
|
|
char buf1[CYG_MAX_PATH + 1];
|
|
|
|
len = GetModuleFileName (NULL, buf1, CYG_MAX_PATH);
|
|
|
|
strlwr (buf1);
|
|
|
|
strlwr (buf);
|
|
|
|
char *p = strchr (buf, ':');
|
|
|
|
if (!p)
|
|
|
|
p = (char *) "gdb.exe -nw";
|
|
|
|
else
|
|
|
|
*p++ = '\0';
|
|
|
|
if (strstr (buf1, buf))
|
|
|
|
{
|
|
|
|
error_start_init (p);
|
|
|
|
try_to_debug ();
|
2004-02-19 06:32:15 +08:00
|
|
|
console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n");
|
|
|
|
DebugBreak ();
|
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
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
void __stdcall
|
|
|
|
dll_crt0_0 ()
|
|
|
|
{
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-13 01:46:37 +08:00
|
|
|
wincap.init ();
|
2004-02-19 06:32:15 +08:00
|
|
|
initial_env ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
char zeros[sizeof (child_proc_info->zero)] = {0};
|
2002-06-11 01:08:09 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
init_console_handler ();
|
|
|
|
init_global_security ();
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
|
|
|
|
GetCurrentProcess (), &hMainProc, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
hMainProc = GetCurrentProcess ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
|
|
|
|
&hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
|
2000-09-11 00:43:47 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
(void) SetErrorMode (SEM_FAILCRITICALERRORS);
|
2002-11-29 15:05:26 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
STARTUPINFO si;
|
|
|
|
GetStartupInfo (&si);
|
|
|
|
child_proc_info = (child_info *) si.lpReserved2;
|
|
|
|
|
|
|
|
if (si.cbReserved2 < EXEC_MAGIC_SIZE || !child_proc_info
|
|
|
|
|| memcmp (child_proc_info->zero, zeros,
|
|
|
|
sizeof (child_proc_info->zero)) != 0)
|
|
|
|
child_proc_info = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((child_proc_info->intro & OPROC_MAGIC_MASK) == OPROC_MAGIC_GENERIC)
|
|
|
|
multiple_cygwin_problem ("proc", child_proc_info->intro, 0);
|
|
|
|
else if (child_proc_info->intro == PROC_MAGIC_GENERIC
|
|
|
|
&& child_proc_info->magic != CHILD_INFO_MAGIC)
|
|
|
|
multiple_cygwin_problem ("proc", child_proc_info->magic,
|
|
|
|
CHILD_INFO_MAGIC);
|
|
|
|
else if (child_proc_info->cygheap != (void *) &_cygheap_start)
|
|
|
|
multiple_cygwin_problem ("cygheap", (DWORD) child_proc_info->cygheap,
|
|
|
|
(DWORD) &_cygheap_start);
|
|
|
|
unsigned should_be_cb = 0;
|
|
|
|
switch (child_proc_info->type)
|
|
|
|
{
|
|
|
|
case _PROC_FORK:
|
2004-09-12 11:47:57 +08:00
|
|
|
user_data->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)
|
|
|
|
should_be_cb = sizeof (child_info);
|
|
|
|
if (should_be_cb != child_proc_info->cb)
|
|
|
|
multiple_cygwin_problem ("proc size", child_proc_info->cb, should_be_cb);
|
|
|
|
else if (sizeof (fhandler_union) != child_proc_info->fhandler_union_cb)
|
|
|
|
multiple_cygwin_problem ("fhandler size", child_proc_info->fhandler_union_cb, sizeof (fhandler_union));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygwin_user_h = child_proc_info->user_h;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
system_printf ("unknown exec type %d", child_proc_info->type);
|
|
|
|
/* intentionally fall through */
|
|
|
|
case _PROC_WHOOPS:
|
|
|
|
child_proc_info = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device::init ();
|
|
|
|
do_global_ctors (&__CTOR_LIST__, 1);
|
|
|
|
cygthread::init ();
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2002-10-15 15:03:45 +08:00
|
|
|
if (!child_proc_info)
|
2002-10-23 04:16:31 +08:00
|
|
|
memory_init ();
|
2002-10-15 15:03:45 +08:00
|
|
|
else
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2002-07-14 05:08:13 +08:00
|
|
|
bool close_ppid_handle = false;
|
|
|
|
bool close_hexec_proc = false;
|
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:
|
2000-09-03 12:16:35 +08:00
|
|
|
alloc_stack (fork_info);
|
2004-07-25 03:18:58 +08:00
|
|
|
cygheap_fixup_in_child (false);
|
2002-10-15 15:03:45 +08:00
|
|
|
memory_init ();
|
2004-09-12 11:47:57 +08:00
|
|
|
set_myself (NULL);
|
2002-10-23 00:18:55 +08:00
|
|
|
close_ppid_handle = !!child_proc_info->pppid_handle;
|
2000-09-03 12:16:35 +08:00
|
|
|
break;
|
2001-10-10 10:32:12 +08:00
|
|
|
case _PROC_SPAWN:
|
2002-07-14 05:08:13 +08:00
|
|
|
/* Have to delay closes until after cygheap is setup */
|
|
|
|
close_hexec_proc = !!spawn_info->hexec_proc;
|
|
|
|
close_ppid_handle = !!child_proc_info->pppid_handle;
|
2000-10-17 07:55:58 +08:00
|
|
|
goto around;
|
2001-10-10 10:32:12 +08:00
|
|
|
case _PROC_EXEC:
|
2000-10-17 07:55:58 +08:00
|
|
|
hexec_proc = spawn_info->hexec_proc;
|
|
|
|
around:
|
2000-09-03 12:16:35 +08:00
|
|
|
HANDLE h;
|
2004-07-25 03:18:58 +08:00
|
|
|
cygheap_fixup_in_child (true);
|
2002-10-15 15:03:45 +08:00
|
|
|
memory_init ();
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!spawn_info->moreinfo->myself_pinfo ||
|
|
|
|
!DuplicateHandle (hMainProc, spawn_info->moreinfo->myself_pinfo,
|
2004-07-25 03:18:58 +08:00
|
|
|
hMainProc, &h, 0, FALSE,
|
2000-10-15 09:37:07 +08:00
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
2000-09-03 12:16:35 +08:00
|
|
|
h = NULL;
|
2004-09-12 11:47:57 +08:00
|
|
|
set_myself (h);
|
2003-09-09 11:11:31 +08:00
|
|
|
__argc = spawn_info->moreinfo->argc;
|
2000-09-03 12:16:35 +08:00
|
|
|
__argv = spawn_info->moreinfo->argv;
|
2000-10-17 07:55:58 +08:00
|
|
|
envp = spawn_info->moreinfo->envp;
|
|
|
|
envc = spawn_info->moreinfo->envc;
|
2004-01-14 23:45:37 +08:00
|
|
|
envp = spawn_info->moreinfo->envp;
|
2004-02-03 05:00:07 +08:00
|
|
|
cygheap->fdtab.fixup_after_exec ();
|
2003-03-20 09:34:53 +08:00
|
|
|
signal_fixup_after_exec ();
|
2000-09-03 12:16:35 +08:00
|
|
|
if (spawn_info->moreinfo->old_title)
|
|
|
|
{
|
|
|
|
old_title = strcpy (title_buf, spawn_info->moreinfo->old_title);
|
|
|
|
cfree (spawn_info->moreinfo->old_title);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-07-14 05:08:13 +08:00
|
|
|
if (close_hexec_proc)
|
|
|
|
CloseHandle (spawn_info->hexec_proc);
|
|
|
|
if (close_ppid_handle)
|
|
|
|
CloseHandle (child_proc_info->pppid_handle);
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-02-12 11:01:58 +08:00
|
|
|
_cygtls::init ();
|
2004-01-14 23:45:37 +08:00
|
|
|
|
|
|
|
/* Initialize events */
|
|
|
|
events_init ();
|
|
|
|
|
|
|
|
cygheap->cwd.init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
static void
|
|
|
|
dll_crt0_1 (char *)
|
|
|
|
{
|
|
|
|
/* According to onno@stack.urc.tue.nl, the exception handler record must
|
|
|
|
be on the stack. */
|
|
|
|
/* FIXME: Verify forked children get their exception handler set up ok. */
|
|
|
|
exception_list cygwin_except_entry;
|
|
|
|
|
|
|
|
check_sanity_and_sync (user_data);
|
|
|
|
malloc_init ();
|
|
|
|
|
|
|
|
/* Initialize SIGSEGV handling, etc. */
|
|
|
|
init_exceptions (&cygwin_except_entry);
|
|
|
|
|
|
|
|
user_data->resourcelocks->Init ();
|
|
|
|
user_data->threadinterface->Init ();
|
2002-07-14 04:00:27 +08:00
|
|
|
ProtectHandle (hMainProc);
|
|
|
|
ProtectHandle (hMainThread);
|
|
|
|
|
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 */
|
|
|
|
if (!user_data->forkee)
|
2004-01-27 02:52:02 +08:00
|
|
|
pthread::init_mainthread ();
|
2002-11-24 21:54:14 +08:00
|
|
|
|
2003-07-30 12:46:07 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
strace.microseconds ();
|
|
|
|
#endif
|
|
|
|
|
2002-07-15 03:15:32 +08:00
|
|
|
/* Initialize debug muto, if DLL is built with --enable-debugging.
|
|
|
|
Need to do this before any helper threads start. */
|
|
|
|
debug_init ();
|
|
|
|
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2001-04-19 05:10:15 +08:00
|
|
|
cygheap->fdtab.vfork_child_fixup ();
|
2001-09-15 08:47:44 +08:00
|
|
|
main_vfork = vfork_storage.create ();
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2000-10-19 08:45:39 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
cygbench ("pre-forkee");
|
2000-02-18 03:38:33 +08:00
|
|
|
if (user_data->forkee)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
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
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Initialize our process table entry. */
|
2000-10-17 07:55:58 +08:00
|
|
|
pinfo_init (envp, envc);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
|
|
|
|
old_title = title_buf;
|
|
|
|
|
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
|
|
|
|
2003-01-25 18:36:46 +08:00
|
|
|
/* Initialize user info. */
|
|
|
|
uinfo_init ();
|
2000-05-22 08:54:22 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Initialize signal/subprocess handling. */
|
|
|
|
sigproc_init ();
|
|
|
|
|
|
|
|
/* Connect to tty. */
|
|
|
|
tty_init ();
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!__argc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-17 07:55:58 +08:00
|
|
|
char *line = GetCommandLineA ();
|
|
|
|
line = strcpy ((char *) alloca (strlen (line) + 1), line);
|
|
|
|
|
2000-12-10 08:45:12 +08:00
|
|
|
if (current_codepage == oem_cp)
|
2001-01-08 12:02:02 +08:00
|
|
|
CharToOemA (line, line);
|
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], '\\')))
|
|
|
|
{
|
2003-12-26 12:40:52 +08:00
|
|
|
char *new_argv0 = (char *) malloc (CYG_MAX_PATH);
|
2000-09-03 12:16:35 +08:00
|
|
|
cygwin_conv_to_posix_path (__argv[0], new_argv0);
|
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;
|
|
|
|
if (cp > __progname && strcasematch (cp, ".exe"))
|
|
|
|
*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);
|
|
|
|
}
|
|
|
|
|
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-18 03:18:21 +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. */
|
|
|
|
wait_for_sigthread ();
|
2000-06-17 03:36:07 +08:00
|
|
|
if (user_data->main)
|
2000-09-03 12:16:35 +08:00
|
|
|
exit (user_data->main (__argc, __argv, *user_data->envptr));
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-12-24 00:26:31 +08:00
|
|
|
struct _reent *
|
|
|
|
initialize_main_tls (char *padding)
|
|
|
|
{
|
|
|
|
if (!_main_tls)
|
|
|
|
{
|
|
|
|
_main_tls = &_my_tls;
|
2004-01-14 23:45:37 +08:00
|
|
|
_main_tls->init_thread (padding, NULL);
|
2003-12-24 00:26:31 +08:00
|
|
|
}
|
|
|
|
return &_main_tls->local_clib;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Wrap the real one, otherwise gdb gets confused about
|
|
|
|
two symbols with the same name, but different addresses.
|
|
|
|
|
|
|
|
UPTR is a pointer to global data that lives on the libc side of the
|
|
|
|
line [if one distinguishes the application from the dll]. */
|
2004-05-29 03:50:07 +08:00
|
|
|
|
2000-07-01 12:26:45 +08:00
|
|
|
extern "C" void __stdcall
|
|
|
|
_dll_crt0 ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2004-01-14 23:45:37 +08:00
|
|
|
extern HANDLE sync_startup;
|
2004-02-25 09:19:13 +08:00
|
|
|
extern unsigned threadfunc_ix;
|
|
|
|
if (threadfunc_ix)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (!sync_startup)
|
2004-02-25 01:13:16 +08:00
|
|
|
system_printf ("internal error: sync_startup not called at start. Expect signal problems.");
|
|
|
|
else
|
2004-01-14 23:45:37 +08:00
|
|
|
{
|
|
|
|
(void) WaitForSingleObject (sync_startup, INFINITE);
|
|
|
|
CloseHandle (sync_startup);
|
|
|
|
}
|
2000-11-14 13:53:32 +08:00
|
|
|
|
2004-02-25 01:13:16 +08:00
|
|
|
if (!threadfunc_ix)
|
|
|
|
system_printf ("internal error: couldn't determine location of thread function on stack. Expect signal problems.");
|
|
|
|
|
2000-07-15 10:48:11 +08:00
|
|
|
main_environ = user_data->envptr;
|
2000-11-14 13:53:32 +08:00
|
|
|
*main_environ = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
char padding[CYGTLS_PADSIZE];
|
2004-09-16 11:49:03 +08:00
|
|
|
_impure_ptr = _GLOBAL_REENT;
|
2003-12-26 12:40:52 +08:00
|
|
|
_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";
|
2004-01-27 02:52:02 +08:00
|
|
|
|
|
|
|
if (child_proc_info && child_proc_info->type == _PROC_FORK)
|
2004-09-12 11:47:57 +08:00
|
|
|
user_data->forkee = true;
|
2004-01-27 02:52:02 +08:00
|
|
|
else
|
|
|
|
__sinit (_impure_ptr);
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
initialize_main_tls (padding);
|
|
|
|
dll_crt0_1 (padding);
|
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. */
|
2000-07-15 10:48:11 +08:00
|
|
|
if (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
|
|
|
}
|
|
|
|
|
2000-06-08 08:55:27 +08:00
|
|
|
/* This must be called by anyone who uses LoadLibrary to load cygwin1.dll */
|
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;
|
|
|
|
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
|
|
|
|
GetCurrentProcess (), &hMainProc, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
hMainProc = GetCurrentProcess ();
|
|
|
|
|
|
|
|
DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
|
|
|
|
&hMainThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
|
|
user_data->magic_biscuit = sizeof (per_process);
|
|
|
|
|
|
|
|
user_data->envptr = &envp;
|
|
|
|
user_data->fmode_ptr = &_fmode;
|
|
|
|
|
2003-12-24 00:26:31 +08:00
|
|
|
dll_crt0_1 (NULL);
|
2000-06-08 08:55:27 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
|
|
|
__main (void)
|
|
|
|
{
|
2003-12-08 06:37:12 +08:00
|
|
|
do_global_ctors (user_data->ctors, false);
|
2003-12-24 00:26:31 +08:00
|
|
|
atexit (do_global_dtors);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
exit_states NO_COPY exit_state;
|
2002-12-26 04:33:21 +08:00
|
|
|
extern CRITICAL_SECTION exit_lock;
|
2002-10-14 02:16: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
|
|
|
|
|
|
|
EnterCriticalSection (&exit_lock);
|
2004-01-14 23:45:37 +08:00
|
|
|
muto::set_exiting_thread ();
|
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;
|
|
|
|
if (!(n & EXIT_REPARENTING))
|
|
|
|
{
|
|
|
|
signal (SIGCHLD, SIG_IGN);
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
signal (SIGQUIT, SIG_IGN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
close_all_files ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_state < ES_SIGPROCTERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_SIGPROCTERMINATE;
|
|
|
|
sigproc_terminate ();
|
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
myself->stopsig = 0;
|
|
|
|
if (exit_state < ES_TITLE)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-10-14 02:16:33 +08:00
|
|
|
exit_state = ES_TITLE;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* restore console title */
|
|
|
|
if (old_title && display_title)
|
|
|
|
set_console_title (old_title);
|
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_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
|
|
|
{
|
2004-01-19 13:46:54 +08:00
|
|
|
siginfo_t si;
|
|
|
|
si.si_signo = -SIGHUP;
|
|
|
|
si.si_code = SI_KERNEL;
|
|
|
|
si.si_pid = si.si_uid = si.si_errno = 0;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_state < ES_TTY_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_TTY_TERMINATE;
|
2000-02-18 03:38:33 +08:00
|
|
|
tty_terminate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
minimal_printf ("winpid %d, exit %d", GetCurrentProcessId (), n);
|
2000-10-15 09:37:07 +08:00
|
|
|
myself->exit (n);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-03-01 10:02:42 +08:00
|
|
|
static muto *atexit_lock;
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
cygwin_atexit (void (*function)(void))
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (!atexit_lock)
|
|
|
|
new_muto (atexit_lock);
|
|
|
|
atexit_lock->acquire ();
|
|
|
|
res = atexit (function);
|
|
|
|
atexit_lock->release ();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
cygwin_exit (int n)
|
|
|
|
{
|
|
|
|
if (atexit_lock)
|
|
|
|
atexit_lock->acquire ();
|
|
|
|
exit (n);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
extern "C" void
|
|
|
|
_exit (int n)
|
|
|
|
{
|
|
|
|
do_exit ((DWORD) n & 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
__api_fatal (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2004-01-14 23:45:37 +08:00
|
|
|
int n = __small_sprintf (buf, "%P (%u): *** ", cygwin_pid (GetCurrentProcessId ()));
|
|
|
|
__small_vsprintf (buf + n, fmt, ap);
|
2000-02-18 03:38:33 +08:00
|
|
|
va_end (ap);
|
2000-10-12 12:38:29 +08:00
|
|
|
strcat (buf, "\n");
|
|
|
|
int len = strlen (buf);
|
|
|
|
DWORD done;
|
|
|
|
(void) WriteFile (GetStdHandle (STD_ERROR_HANDLE), buf, len, &done, 0);
|
|
|
|
|
|
|
|
/* Make sure that the message shows up on the screen, too, since this is
|
|
|
|
a serious error. */
|
|
|
|
if (GetFileType (GetStdHandle (STD_ERROR_HANDLE)) != FILE_TYPE_CHAR)
|
|
|
|
{
|
2002-09-19 23:12:48 +08:00
|
|
|
HANDLE h = CreateFile ("CONOUT$", GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE | FILE_SHARE_WRITE,
|
|
|
|
&sec_none, OPEN_EXISTING, 0, 0);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
2000-10-12 12:38:29 +08:00
|
|
|
(void) WriteFile (h, buf, len, &done, 0);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* We are going down without mercy. Make sure we reset
|
|
|
|
our process_state. */
|
2000-07-06 07:46:44 +08:00
|
|
|
sigproc_terminate ();
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
(void) try_to_debug ();
|
|
|
|
#endif
|
2000-10-15 09:37:07 +08:00
|
|
|
myself->exit (1);
|
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
|
|
|
|
|
|
|
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
|
2001-12-26 12:53:34 +08:00
|
|
|
api_fatal ("%s version mismatch detected - %p/%p.\n\
|
2001-10-10 07:28:24 +08:00
|
|
|
You have multiple copies of cygwin1.dll on your system.\n\
|
|
|
|
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\
|
|
|
|
installed the cygwin distribution.", 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)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_BENCH", buf, sizeof (buf)))
|
|
|
|
small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
|
|
|
|
}
|
|
|
|
#endif
|