2000-02-17 20:38:33 +01:00
|
|
|
/* dcrt0.cc -- essentially the main() for the Cygwin dll
|
|
|
|
|
2002-01-13 21:03:03 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 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>
|
|
|
|
#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>
|
2001-10-16 01:39:33 +02:00
|
|
|
#include <errno.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-04-18 23:10:15 +02:00
|
|
|
#include "fhandler.h"
|
2001-06-04 03:28:09 +02:00
|
|
|
#include "path.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"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "perthread.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"
|
2002-08-01 18:20:31 +02:00
|
|
|
#include "cygthread.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
|
|
|
|
2002-08-01 18:20:31 +02:00
|
|
|
HANDLE NO_COPY hMainProc;
|
|
|
|
HANDLE NO_COPY hMainThread;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-05-18 05:20:01 +02:00
|
|
|
sigthread NO_COPY mainthread; // ID of the main thread
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
per_thread_waitq NO_COPY waitq_storage;
|
|
|
|
per_thread_vfork NO_COPY vfork_storage;
|
|
|
|
per_thread_signal_dispatch NO_COPY signal_dispatch_storage;
|
|
|
|
|
|
|
|
per_thread NO_COPY *threadstuff[] = {&waitq_storage,
|
|
|
|
&vfork_storage,
|
|
|
|
&signal_dispatch_storage,
|
|
|
|
NULL};
|
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
BOOL display_title;
|
|
|
|
BOOL strip_title_path;
|
2000-02-17 20:38:33 +01:00
|
|
|
BOOL allow_glob = TRUE;
|
2000-12-10 01:45:12 +01:00
|
|
|
codepage_type current_codepage = ansi_cp;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
int 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;
|
2001-10-10 04:32:12 +02:00
|
|
|
unsigned NO_COPY _cygwin_testing_magic;
|
2001-09-03 04:13:05 +02:00
|
|
|
|
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;
|
2000-07-15 04:48:11 +02:00
|
|
|
char ***main_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;
|
2001-02-20 03:53:55 +01:00
|
|
|
struct _reent reent_data = _REENT_INIT(reent_data);
|
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},
|
|
|
|
/* forkee */ 0,
|
|
|
|
/* 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,
|
|
|
|
/* impure_ptr */ &reent_data,
|
|
|
|
};
|
2001-09-06 06:41:59 +02:00
|
|
|
bool ignore_case_with_glob;
|
2001-05-08 17:16:49 +02: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
|
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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!force)
|
|
|
|
{
|
|
|
|
if (user_data->forkee || user_data->run_ctors_p)
|
2000-09-03 06:16:35 +02:00
|
|
|
return; // inherit constructed stuff from parent pid
|
2000-02-17 20:38:33 +01:00
|
|
|
user_data->run_ctors_p = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run ctors backwards, so skip the first entry and find how many
|
|
|
|
there are, then run them. */
|
|
|
|
|
|
|
|
void (**pfunc)() = in_pfunc;
|
|
|
|
|
|
|
|
while (*++pfunc)
|
|
|
|
;
|
|
|
|
while (--pfunc > in_pfunc)
|
|
|
|
(*pfunc) ();
|
|
|
|
|
2000-07-01 05:51:55 +02:00
|
|
|
if (user_data->magic_biscuit == SIZEOF_PER_PROCESS)
|
2000-02-17 20:38:33 +01:00
|
|
|
atexit (do_global_dtors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
|
|
|
static int __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);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_size = strlen (cmd) + size + 2;
|
|
|
|
char *tmp = (char *) malloc (new_size);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
debug_printf ("malloc failed, %E");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp[size++] = ' ';
|
|
|
|
strcpy (tmp + size, cmd);
|
|
|
|
cmd = tmp;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if ((p = strchr (cmd, quote)) != NULL)
|
|
|
|
strcpy (p, p + 1);
|
2000-09-13 21:57:36 +02:00
|
|
|
else
|
|
|
|
p = strchr (cmd, '\0');
|
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)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
if (p->api_major != cygwin_version.api_major)
|
|
|
|
api_fatal ("cygwin DLL and APP are out of sync -- API version mismatch %d < %d",
|
|
|
|
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;
|
2002-06-26 06:21:01 +02:00
|
|
|
static MEMORY_BASIC_INFORMATION NO_COPY sm;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-15 22:34:49 +02:00
|
|
|
#define CYGWIN_GUARD ((wincap.has_page_guard ()) ? \
|
2002-09-30 17:17:44 +02:00
|
|
|
PAGE_EXECUTE_READWRITE|PAGE_GUARD : PAGE_NOACCESS)
|
2001-07-29 04:33:30 +02:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
// __inline__ void
|
|
|
|
extern void
|
2000-02-17 20:38:33 +01:00
|
|
|
alloc_stack_hard_way (child_info_fork *ci, volatile char *b)
|
|
|
|
{
|
|
|
|
void *new_stack_pointer;
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
2000-09-03 06:16:35 +02:00
|
|
|
void *newbase;
|
|
|
|
int newlen;
|
|
|
|
LPBYTE curbot = (LPBYTE) sm.BaseAddress + sm.RegionSize;
|
|
|
|
bool noguard;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-09-03 06:16:35 +02: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-17 20:38:33 +01: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 06:16:35 +02:00
|
|
|
if (!noguard)
|
|
|
|
{
|
|
|
|
m.BaseAddress = (LPVOID)((DWORD)m.BaseAddress - 1);
|
|
|
|
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");
|
|
|
|
ci->stacktop = m.BaseAddress;
|
|
|
|
*b = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extend the stack prior to fork longjmp */
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
static void
|
2000-02-17 20:38:33 +01:00
|
|
|
alloc_stack (child_info_fork *ci)
|
|
|
|
{
|
|
|
|
/* 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];
|
|
|
|
|
2001-10-10 04:32:12 +02:00
|
|
|
if (!VirtualQuery ((LPCVOID) &b, &sm, sizeof sm))
|
|
|
|
api_fatal ("fork: couldn't get stack info, %E");
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-10-10 04:32:12 +02:00
|
|
|
if (sm.AllocationBase != ci->stacktop)
|
|
|
|
alloc_stack_hard_way (ci, b + sizeof (b) - 1);
|
|
|
|
else
|
|
|
|
ci->stacksize = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NO_COPY int mypid = 0;
|
2001-09-06 06:41:59 +02:00
|
|
|
int _declspec(dllexport) __argc;
|
|
|
|
char _declspec(dllexport) **__argv;
|
2001-09-15 02:47:44 +02:00
|
|
|
vfork_save NO_COPY *main_vfork = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-05-18 23:30:30 +02:00
|
|
|
void
|
|
|
|
sigthread::init (const char *s)
|
|
|
|
{
|
2000-11-07 00:12:05 +01:00
|
|
|
InitializeCriticalSection (&lock);
|
2000-10-19 19:57:28 +02:00
|
|
|
id = GetCurrentThreadId ();
|
2000-05-18 23:30:30 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +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. */
|
|
|
|
static void
|
|
|
|
dll_crt0_1 ()
|
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
/* 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;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-01-28 06:51:15 +01:00
|
|
|
/* Initialize SIGSEGV handling, etc. */
|
2000-09-03 06:16:35 +02:00
|
|
|
init_exceptions (&cygwin_except_entry);
|
|
|
|
|
|
|
|
/* Set the os_being_run global. */
|
* Makefile.in: Build wincap.o.
* wincap.cc: New file.
* wincap.h: Ditto.
* autoload.cc: Add dynamic load statement for `CreateHardLinkA'.
* dcrt0.cc (os_being_run): Eliminated.
(osname): Ditto.
(iswinnt): Ditto.
(set_os_type): Ditto.
(dll_crt0_1): Call wincap.init() instead of set_os_type().
(_dll_crt0): Ditto.
* environ.cc (set_chunksize): New function.
(parse_thing): `forkchunk' setting now invokes function `set_chunksize'.
* fork.cc (chunksize): Eliminated. Moved to be member of wincap.
* host_dependent.h: Removed.
* syscalls.cc (_link): Try using `CreateHardLinkA' first, if available.
* cygheap.cc, dcrt0.cc, delqueue.cc, dir.cc,
environ.cc, fhandler.cc, fhandler.h, fhandler_console.cc,
fhandler_mem.cc, fork.cc, mmap.cc, net.cc, pinfo.cc, pinfo.h,
security.cc, syscalls.cc, sysconf.cc, syslog.cc, thread.cc,
times.cc, tty.cc, uinfo.cc, uname.cc, winsup.h: Use new wincap
capability check throughout.
* winsup.h: Include wincap.h. Eliminate extern declarations of
`os_being_run' and `iswinnt'. Eliminate `os_type" definition.
* include/cygwin/version.h: Bump version to 1.3.4.
2001-09-12 19:46:37 +02:00
|
|
|
wincap.init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
check_sanity_and_sync (user_data);
|
|
|
|
|
2002-06-10 19:08:09 +02:00
|
|
|
do_global_ctors (&__CTOR_LIST__, 1);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Nasty static stuff needed by newlib -- point to a local copy of
|
|
|
|
the reent stuff.
|
|
|
|
Note: this MUST be done here (before the forkee code) as the
|
|
|
|
fork copy code doesn't copy the data in libccrt0.cc (that's why we
|
|
|
|
pass in the per_process struct into the .dll from libccrt0). */
|
|
|
|
|
|
|
|
_impure_ptr = &reent_data;
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
user_data->resourcelocks->Init ();
|
2000-10-17 01:55:58 +02:00
|
|
|
user_data->threadinterface->Init (user_data->forkee);
|
2000-09-10 18:43:47 +02:00
|
|
|
|
2000-11-07 00:12:05 +01:00
|
|
|
mainthread.init ("mainthread"); // For use in determining if signals
|
|
|
|
// should be blocked.
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
int envc = 0;
|
2000-09-03 06:16:35 +02:00
|
|
|
char **envp = NULL;
|
|
|
|
|
2002-10-15 09:03:45 +02:00
|
|
|
if (!child_proc_info)
|
2002-10-22 22:16:31 +02:00
|
|
|
memory_init ();
|
2002-10-15 09:03:45 +02:00
|
|
|
else
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2002-07-13 23:08:13 +02:00
|
|
|
bool close_ppid_handle = false;
|
|
|
|
bool close_hexec_proc = false;
|
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:
|
2000-09-03 06:16:35 +02:00
|
|
|
alloc_stack (fork_info);
|
2002-10-21 03:00:58 +02:00
|
|
|
cygheap_fixup_in_child (0);
|
2002-10-15 09:03:45 +02:00
|
|
|
memory_init ();
|
|
|
|
set_myself (mypid);
|
2002-10-22 18:18:55 +02:00
|
|
|
close_ppid_handle = !!child_proc_info->pppid_handle;
|
2000-09-03 06:16:35 +02:00
|
|
|
break;
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_SPAWN:
|
2002-07-13 23:08:13 +02: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 01:55:58 +02:00
|
|
|
goto around;
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_EXEC:
|
2000-10-17 01:55:58 +02:00
|
|
|
hexec_proc = spawn_info->hexec_proc;
|
|
|
|
around:
|
2000-09-03 06:16:35 +02:00
|
|
|
HANDLE h;
|
2002-06-15 23:59:32 +02:00
|
|
|
cygheap_fixup_in_child (1);
|
2002-10-15 09:03:45 +02:00
|
|
|
memory_init ();
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!spawn_info->moreinfo->myself_pinfo ||
|
|
|
|
!DuplicateHandle (hMainProc, spawn_info->moreinfo->myself_pinfo,
|
2000-10-15 03:37:07 +02:00
|
|
|
hMainProc, &h, 0, 0,
|
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
|
2000-09-03 06:16:35 +02:00
|
|
|
h = NULL;
|
|
|
|
set_myself (mypid, h);
|
|
|
|
__argc = spawn_info->moreinfo->argc;
|
|
|
|
__argv = spawn_info->moreinfo->argv;
|
2000-10-17 01:55:58 +02:00
|
|
|
envp = spawn_info->moreinfo->envp;
|
|
|
|
envc = spawn_info->moreinfo->envc;
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab.fixup_after_exec (spawn_info->parent);
|
2000-10-17 01:55:58 +02:00
|
|
|
signal_fixup_after_exec (child_proc_info->type == PROC_SPAWN);
|
2000-09-03 06:16:35 +02:00
|
|
|
CloseHandle (spawn_info->parent);
|
|
|
|
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-13 23:08:13 +02:00
|
|
|
if (close_hexec_proc)
|
|
|
|
CloseHandle (spawn_info->hexec_proc);
|
|
|
|
if (close_ppid_handle)
|
|
|
|
CloseHandle (child_proc_info->pppid_handle);
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
ProtectHandle (hMainProc);
|
|
|
|
ProtectHandle (hMainThread);
|
2002-10-22 22:16:31 +02:00
|
|
|
cygthread::init ();
|
2002-07-13 22:00:27 +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 ();
|
|
|
|
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab.vfork_child_fixup ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
(void) SetErrorMode (SEM_FAILCRITICALERRORS);
|
|
|
|
|
|
|
|
/* Initialize events. */
|
|
|
|
events_init ();
|
|
|
|
|
2001-04-17 05:52:08 +02:00
|
|
|
cygheap->cwd.init ();
|
2001-09-15 02:47:44 +02:00
|
|
|
main_vfork = vfork_storage.create ();
|
2000-10-19 02:45:39 +02:00
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
cygbench ("pre-forkee");
|
2000-02-17 20:38:33 +01: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 06:16:35 +02:00
|
|
|
if (fork_info->stacksize)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
asm ("movl %0,%%fs:4" : : "r" (fork_info->stackbottom));
|
|
|
|
asm ("movl %0,%%fs:8" : : "r" (fork_info->stacktop));
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
longjmp (fork_info->jmp, fork_info->cygpid);
|
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
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
/* Initialize our process table entry. */
|
2000-10-17 01:55:58 +02:00
|
|
|
pinfo_init (envp, envc);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (!old_title && GetConsoleTitle (title_buf, TITLESIZE))
|
|
|
|
old_title = title_buf;
|
|
|
|
|
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
|
|
|
|
2002-06-14 20:01:21 +02:00
|
|
|
/* Initialize uid, gid if necessary. */
|
|
|
|
if (child_proc_info == NULL || spawn_info->moreinfo->uid == ILLEGAL_UID)
|
|
|
|
uinfo_init ();
|
2000-05-22 02:54:22 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Initialize signal/subprocess handling. */
|
|
|
|
sigproc_init ();
|
|
|
|
|
|
|
|
/* Connect to tty. */
|
|
|
|
tty_init ();
|
|
|
|
|
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], '\\')))
|
|
|
|
{
|
|
|
|
char *new_argv0 = (char *) alloca (MAX_PATH);
|
|
|
|
cygwin_conv_to_posix_path (__argv[0], new_argv0);
|
|
|
|
__argv[0] = new_argv0;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
cygwin_finished_initializing = 1;
|
|
|
|
/* 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 */
|
|
|
|
ignore_case_with_glob = FALSE;
|
|
|
|
|
2000-07-17 21:18:21 +02: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. */
|
2002-08-11 21:19:29 +02:00
|
|
|
wait_for_sigthread ();
|
2000-07-17 21:18:21 +02: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);
|
2000-06-16 21:36:07 +02:00
|
|
|
if (user_data->main)
|
2000-09-03 06:16:35 +02:00
|
|
|
exit (user_data->main (__argc, __argv, *user_data->envptr));
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-07-13 23:08:13 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
void
|
|
|
|
break_here ()
|
|
|
|
{
|
|
|
|
debug_printf ("break here");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-10 04:32:12 +02:00
|
|
|
void
|
|
|
|
initial_env ()
|
|
|
|
{
|
2002-07-13 23:08:13 +02:00
|
|
|
DWORD len;
|
2001-10-10 04:32:12 +02:00
|
|
|
char buf[MAX_PATH + 1];
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
|
|
|
|
{
|
2002-08-27 06:10:54 +02:00
|
|
|
DWORD ms = atoi (buf);
|
2002-07-13 23:08:13 +02:00
|
|
|
buf[0] = '\0';
|
|
|
|
len = GetModuleFileName (NULL, buf, MAX_PATH);
|
2002-10-14 22:25:52 +02:00
|
|
|
console_printf ("Sleeping %d, pid %u %s\n", ms, GetCurrentProcessId (), buf);
|
2002-08-27 06:10:54 +02:00
|
|
|
Sleep (ms);
|
2001-10-10 04:32:12 +02:00
|
|
|
}
|
2002-07-13 23:08:13 +02:00
|
|
|
if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
|
|
|
|
{
|
|
|
|
char buf1[MAX_PATH + 1];
|
|
|
|
len = GetModuleFileName (NULL, buf1, MAX_PATH);
|
2002-07-15 05:48:52 +02:00
|
|
|
strlwr (buf1);
|
|
|
|
strlwr (buf);
|
2002-07-13 23:08:13 +02:00
|
|
|
char *p = strchr (buf, '=');
|
|
|
|
if (!p)
|
2002-07-14 21:15:32 +02:00
|
|
|
p = (char *) "gdb.exe -nw";
|
2002-07-13 23:08:13 +02:00
|
|
|
else
|
|
|
|
*p++ = '\0';
|
|
|
|
if (strstr (buf1, buf))
|
|
|
|
{
|
|
|
|
error_start_init (p);
|
|
|
|
try_to_debug ();
|
|
|
|
break_here ();
|
|
|
|
}
|
|
|
|
}
|
2001-10-10 04:32:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
|
|
|
|
{
|
|
|
|
_cygwin_testing = 1;
|
|
|
|
if ((len = GetModuleFileName (cygwin_hmodule, buf, MAX_PATH))
|
|
|
|
&& len > sizeof ("new-cygwin1.dll")
|
|
|
|
&& strcasematch (buf + len - sizeof ("new-cygwin1.dll"),
|
|
|
|
"\\new-cygwin1.dll"))
|
|
|
|
_cygwin_testing_magic = 0x10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01: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]. */
|
|
|
|
|
2000-07-01 06:26:45 +02:00
|
|
|
extern "C" void __stdcall
|
|
|
|
_dll_crt0 ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-11-28 01:06:35 +01:00
|
|
|
DECLARE_TLS_STORAGE;
|
2001-10-10 04:32:12 +02:00
|
|
|
initial_env ();
|
2000-11-14 06:53:32 +01:00
|
|
|
char zeros[sizeof (fork_info->zero)] = {0};
|
2002-06-15 23:59:32 +02:00
|
|
|
static NO_COPY STARTUPINFO si;
|
2000-11-14 06:53:32 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
strace.microseconds ();
|
|
|
|
#endif
|
|
|
|
|
2000-07-15 04:48:11 +02:00
|
|
|
main_environ = user_data->envptr;
|
2000-11-14 06:53:32 +01:00
|
|
|
*main_environ = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-01-10 04:21:27 +01:00
|
|
|
early_stuff_init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentProcess (),
|
|
|
|
GetCurrentProcess (), &hMainProc, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
hMainProc = GetCurrentProcess ();
|
|
|
|
|
|
|
|
DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
|
2001-09-15 06:36:43 +02:00
|
|
|
&hMainThread, 0, false, DUPLICATE_SAME_ACCESS);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
GetStartupInfo (&si);
|
2002-06-15 23:59:32 +02:00
|
|
|
child_proc_info = (child_info *) si.lpReserved2;
|
2002-06-26 07:37:29 +02:00
|
|
|
if (si.cbReserved2 < EXEC_MAGIC_SIZE || !child_proc_info
|
|
|
|
|| memcmp (child_proc_info->zero, zeros, sizeof (zeros)) != 0)
|
|
|
|
child_proc_info = NULL;
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-06-15 23:59:32 +02:00
|
|
|
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)
|
2002-08-27 06:10:54 +02:00
|
|
|
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);
|
2001-12-26 22:35:16 +01:00
|
|
|
unsigned should_be_cb = 0;
|
2002-06-15 23:59:32 +02:00
|
|
|
switch (child_proc_info->type)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_FORK:
|
2002-06-15 23:59:32 +02:00
|
|
|
user_data->forkee = child_proc_info->cygpid;
|
2001-12-26 22:35:16 +01:00
|
|
|
should_be_cb = sizeof (child_info_fork);
|
2002-01-13 17:55:32 +01:00
|
|
|
/* fall through */;
|
2001-10-10 04:32:12 +02:00
|
|
|
case _PROC_SPAWN:
|
|
|
|
case _PROC_EXEC:
|
2001-12-26 22:35:16 +01:00
|
|
|
if (!should_be_cb)
|
|
|
|
should_be_cb = sizeof (child_info);
|
2002-06-15 23:59:32 +02:00
|
|
|
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));
|
2001-12-26 22:35:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cygwin_mount_h = child_proc_info->mount_h;
|
|
|
|
mypid = child_proc_info->cygpid;
|
|
|
|
break;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
default:
|
2002-06-15 23:59:32 +02:00
|
|
|
system_printf ("unknown exec type %d", child_proc_info->type);
|
2002-08-27 06:10:54 +02:00
|
|
|
/* intentionally fall through */
|
|
|
|
case _PROC_WHOOPS:
|
2002-06-15 23:59:32 +02:00
|
|
|
child_proc_info = NULL;
|
2001-04-09 03:23:27 +02:00
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dll_crt0_1 ();
|
|
|
|
}
|
|
|
|
|
2000-07-01 05:51:55 +02:00
|
|
|
void
|
|
|
|
dll_crt0 (per_process *uptr)
|
|
|
|
{
|
2001-11-28 01:06:35 +01:00
|
|
|
DECLARE_TLS_STORAGE;
|
2000-07-01 05:51:55 +02:00
|
|
|
/* Set the local copy of the pointer into the user space. */
|
2000-07-15 04:48:11 +02:00
|
|
|
if (uptr && uptr != user_data)
|
2000-07-06 01:46:44 +02:00
|
|
|
{
|
|
|
|
memcpy (user_data, uptr, per_process_overwrite);
|
|
|
|
*(user_data->impure_ptr_ptr) = &reent_data;
|
|
|
|
}
|
2000-07-01 06:26:45 +02:00
|
|
|
_dll_crt0 ();
|
2000-07-01 05:51:55 +02:00
|
|
|
}
|
|
|
|
|
2000-06-08 02:55:27 +02:00
|
|
|
/* This must be called by anyone who uses LoadLibrary to load cygwin1.dll */
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
dll_crt0_1 ();
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" void
|
|
|
|
__main (void)
|
|
|
|
{
|
|
|
|
do_global_ctors (user_data->ctors, FALSE);
|
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
enum exit_states
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-10-13 20:16:33 +02:00
|
|
|
ES_NOT_EXITING = 0,
|
|
|
|
ES_THREADTERM,
|
|
|
|
ES_SIGNAL,
|
|
|
|
ES_CLOSEALL,
|
|
|
|
ES_SIGPROCTERMINATE,
|
|
|
|
ES_TITLE,
|
|
|
|
ES_HUP_PGRP,
|
|
|
|
ES_HUP_SID,
|
|
|
|
ES_TTY_TERMINATE,
|
|
|
|
ES_EVENTS_TERMINATE
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
exit_states NO_COPY exit_state;
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" void __stdcall
|
|
|
|
do_exit (int status)
|
|
|
|
{
|
|
|
|
UINT n = (UINT) status;
|
|
|
|
|
2002-10-10 07:23:23 +02:00
|
|
|
syscall_printf ("do_exit (%d)", n);
|
|
|
|
|
|
|
|
vfork_save *vf = vfork_storage.val ();
|
|
|
|
if (vf != NULL && vf->pid < 0)
|
|
|
|
vf->restore_exit (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;
|
|
|
|
if (!(n & EXIT_REPARENTING))
|
|
|
|
{
|
|
|
|
signal (SIGCHLD, SIG_IGN);
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
signal (SIGQUIT, SIG_IGN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
close_all_files ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_state < ES_SIGPROCTERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_SIGPROCTERMINATE;
|
|
|
|
sigproc_terminate ();
|
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
myself->stopsig = 0;
|
|
|
|
if (exit_state < ES_TITLE)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-10-13 20:16:33 +02:00
|
|
|
exit_state = ES_TITLE;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* restore console title */
|
|
|
|
if (old_title && display_title)
|
|
|
|
set_console_title (old_title);
|
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_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
|
|
|
{
|
|
|
|
sigproc_printf ("%d == pgrp %d, send SIG{HUP,CONT} to stopped children",
|
|
|
|
myself->pid, myself->pgid);
|
|
|
|
kill_pgrp (myself->pgid, -SIGHUP);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (exit_state < ES_TTY_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_TTY_TERMINATE;
|
2000-02-17 20:38:33 +01:00
|
|
|
tty_terminate ();
|
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
if (exit_state < ES_EVENTS_TERMINATE)
|
|
|
|
{
|
|
|
|
exit_state = ES_EVENTS_TERMINATE;
|
|
|
|
events_terminate ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
minimal_printf ("winpid %d, exit %d", GetCurrentProcessId (), n);
|
2000-10-15 03:37:07 +02:00
|
|
|
myself->exit (n);
|
2000-02-17 20:38:33 +01: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);
|
|
|
|
__small_vsprintf (buf, fmt, ap);
|
|
|
|
va_end (ap);
|
2000-10-12 06:38:29 +02: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 17:12:48 +02: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 06:38:29 +02:00
|
|
|
(void) WriteFile (h, buf, len, &done, 0);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* We are going down without mercy. Make sure we reset
|
|
|
|
our process_state. */
|
2000-07-06 01:46:44 +02:00
|
|
|
sigproc_terminate ();
|
2000-02-17 20:38:33 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
(void) try_to_debug ();
|
|
|
|
#endif
|
2000-10-15 03:37:07 +02:00
|
|
|
myself->exit (1);
|
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
|
2001-12-26 05:53:34 +01:00
|
|
|
api_fatal ("%s version mismatch detected - %p/%p.\n\
|
2001-10-10 01:28:24 +02: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 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\
|
|
|
|
installed the cygwin distribution.", 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
|