* exceptions.cc (set_console_handler): Don't allocate

console_handler_thread_waiter.  It is obsolete.
(ctrl_c_handler): Don't use console_handler_thread_waiter.
* path.cc (hash_path_name): Fix handling of relative names.  Make case
insensitive.
* path.h (suffix_info): Use initializers.
* pinfo.h (_pinfo): Avoid initializers for null case.
* resource.cc (fill_rusage): Zero rest of rusage structure.
* security.cc (set_process_privileges): Don't reopen parent process.  Just use
hMainProc.
* signal.cc (signal): Track when a signal handler has been used.
(sigaction): Ditto.
* sigproc.cc (pchildren): Use default initializer.
(zombies): Ditto.
(sigproc_terminate): Avoid closing handles that will be closed on exit anyway.
(wait_sig): Send signal to "parent" on EXECing, not FORKing.
(wait_subproc): Send SIGCHLD here rather than in proc_wait to avoid potential
muto conflicts.
* sigproc.h (sigthread): Don't initialize to zero.  It's the default.
* spawn.cc (spawn_guts): Fill in resources from exec parent prior to
termination.
* sync.h (muto): Don't initialize to zero.
* syscalls.cc (close_all_files): Use one lock around entire loop and call
fhandler close/release stuff directly.
(_read): Don't use ready_for_read if there are not signal handlers active.
* dcrt0.cc (dll_crt0_1): Fix display of "title".
(do_exit): Use pinfo exit method to exit.
(__api_fatal): Ditto.
* exceptions.cc (signal_exit): Ditto.
* fork.cc (fork_child): Remove debugging stuff.  Use pinfo_fixup_after fork in
place of exec_fixup_after_fork.
* pinfo.cc (pinfo_fixup_after_fork): New method.
(pinfo_fixup_in_spawned_child): Ditto.
(_pinfo::exit): New method.
(_pinfo::init): Remove recursion.  Detect pathological case where pinfo
structure already exists for new pid.
* pinfo.h (_pinfo): Reorganize slightly.  Add new method and new function
declarations.
* sigproc.cc (proc_exists): Previous simplification was a little to simple.
Try harder to detect if a process exists.
(proc_terminate): Use PID_EXITED setting to determine if process is still
around.
(WFSO): Remove debugging statement.
(WFMO): Ditto.
* spawn.cc (exec_fixup_after_fork): Eliminate.
(spawn_guts): Always set old_title to NULL.  Is it really needed?  Move
hexec_proc to pinfo.cc.  Call pinfo_fixup_in_spawned_child to eliminate handle
link after a spawn.
* include/sys/cygwin.h: Remove PID_NOT_IN_USE.  Add PID_EXITED.
This commit is contained in:
Christopher Faylor
2000-10-15 01:37:07 +00:00
parent fdc614360f
commit 1dc16fc74b
19 changed files with 359 additions and 277 deletions

View File

@ -24,11 +24,41 @@ details. */
#include "perprocess.h"
#include "environ.h"
#include "security.h"
#include <assert.h>
static char NO_COPY pinfo_dummy[sizeof(pinfo)] = {0};
pinfo NO_COPY myself ((_pinfo *)&pinfo_dummy); // Avoid myself != NULL checks
static HANDLE hexec_proc = NULL;
void __stdcall
pinfo_fixup_after_fork ()
{
if (hexec_proc)
CloseHandle (hexec_proc);
if (!DuplicateHandle (hMainProc, hMainProc, hMainProc, &hexec_proc, 0,
TRUE, DUPLICATE_SAME_ACCESS))
{
system_printf ("couldn't save current process handle %p, %E", hMainProc);
hexec_proc = NULL;
}
}
void __stdcall
pinfo_fixup_in_spawned_child (HANDLE hchild)
{
HANDLE h;
if (!hexec_proc)
return;
if (!DuplicateHandle (hchild, hexec_proc, hMainProc, &h, 0, TRUE,
DUPLICATE_CLOSE_SOURCE))
system_printf ("couldn't close handle %p in child, %E", hexec_proc);
else
CloseHandle (h);
}
/* Initialize the process table.
This is done once when the dll is first loaded. */
@ -96,6 +126,14 @@ pinfo_init (char **envp)
debug_printf ("pid %d, pgid %d", myself->pid, myself->pgid);
}
void
_pinfo::exit (UINT n)
{
process_state = PID_EXITED;
sigproc_printf ("Calling ExitProcess %d", n);
ExitProcess (n);
}
struct sigaction&
_pinfo::getsig(int sig)
{
@ -185,66 +223,82 @@ pinfo::init (pid_t n, DWORD create, HANDLE in_h)
return;
}
int created;
char mapname[MAX_PATH];
__small_sprintf (mapname, "cygpid.%x", n);
int mapsize;
if (create & PID_EXECED)
mapsize = PINFO_REDIR_SIZE;
else
mapsize = sizeof (_pinfo);
if (in_h)
for (int i = 0; i < 10; i++)
{
h = in_h;
created = 0;
}
else if (!create)
{
/* CGF FIXME -- deal with inheritance after an exec */
h = OpenFileMappingA (FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapname);
created = 0;
}
else
{
h = CreateFileMapping ((HANDLE) 0xffffffff, &sec_all_nih,
PAGE_READWRITE, 0, mapsize, mapname);
created = h && GetLastError () != ERROR_ALREADY_EXISTS;
}
int created;
char mapname[MAX_PATH];
__small_sprintf (mapname, "cygpid.%x", n);
if (!h)
{
if (create)
__seterrno ();
procinfo = NULL;
return;
}
int mapsize;
if (create & PID_EXECED)
mapsize = PINFO_REDIR_SIZE;
else
mapsize = sizeof (_pinfo);
ProtectHandle1 (h, pinfo_shared_handle);
procinfo = (_pinfo *) MapViewOfFile (h, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
if (in_h)
{
h = in_h;
created = 0;
}
else if (!create)
{
/* CGF FIXME -- deal with inheritance after an exec */
h = OpenFileMappingA (FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapname);
created = 0;
}
else
{
h = CreateFileMapping ((HANDLE) 0xffffffff, &sec_all_nih,
PAGE_READWRITE, 0, mapsize, mapname);
created = h && GetLastError () != ERROR_ALREADY_EXISTS;
}
if (procinfo->process_state & PID_EXECED)
{
pid_t realpid = procinfo->pid;
debug_printf ("execed process windows pid %d, cygwin pid %d", n, realpid);
release ();
if (realpid == n)
api_fatal ("retrieval of execed process info for pid %d failed due to recursion.", n);
return init (realpid);
}
if (!h)
{
if (create)
__seterrno ();
procinfo = NULL;
return;
}
if (created)
{
if (!(create & PID_EXECED))
procinfo = (_pinfo *) MapViewOfFile (h, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
ProtectHandle1 (h, pinfo_shared_handle);
if (procinfo->process_state & PID_EXECED)
{
assert (!i);
pid_t realpid = procinfo->pid;
debug_printf ("execed process windows pid %d, cygwin pid %d", n, realpid);
if (realpid == n)
api_fatal ("retrieval of execed process info for pid %d failed due to recursion.", n);
n = realpid;
release ();
continue;
}
/* In certain rare, pathological cases, it is possible for the shared
memory region to exist for a while after a process has exited. This
should only be a brief occurrence, so rather than introduce some kind
of locking mechanism, just loop. FIXME: I'm sure I'll regret doing it
this way at some point. */
if (i < 9 && !created && create && (procinfo->process_state & PID_EXITED))
{
Sleep (5);
release ();
continue;
}
if (!created)
/* nothing */;
else if (!(create & PID_EXECED))
procinfo->pid = n;
else
{
procinfo->pid = myself->pid;
procinfo->process_state |= PID_IN_USE | PID_EXECED;
}
break;
}
destroy = 1;
}