mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-15 11:00:04 +08:00
2d1d1eb1e4
compiler bug. * autoload.cc (TryEnterCriticalSection): Remove. * dcrt0.cc (dll_crt0_0): Delete inappropriate setting of _my_tls.stackptr to NULL since it has really bad consequences. Make 'si' an automatic variable. * cygtls.cc (_threadinfo::init_thread): Correct thinko which caused thread list to be allocated every time. * cygtls.h (CYGTLS_PADSIZE): Define as const int. * sync.h: Make multiple inclusion safe. (muto::next): Eliminate. (muto::exiting_thread): New variable. (muto::set_exiting_thread): New function. (new_muto): Change to use different section for mutos since c++ give inexplicable warning in some cases otherwise. (new_muto1): Ditto. * dcrt0.cc (do_exit): Call muto::set_exiting_thread here. * sync.cc (muto_start): Eliminate. (muto::acquire): Always give exiting thread a lock. Never give thread a lock if exiting. (muto::release): Ditto for releasing. * dtable.cc (dtable::init_lock): Unline function and define here. * dtable.h (lock_cs): Define as a muto since critical sections seem to work oddly on Windows Me. (lock): Accommodate switch to muto. (unlock): Ditto. * exceptions.cc (setup_handler): Don't worry about acquiring mutos since that hasn't mattered for a long time. (signal_exit): Ditto: muto stuff will be handled automatically on exit now. * Makefile.in (DLL_IMPORTS): Link advapi32 to ensure proper DLL initialization. * autoload.cc (RegCloseKey): Arbitrarily choose this function as a "seed" to pull the advapi32 link library in. So, comment out the autoloading. * cygtls.cc (_threadinfo::init_thread): Just clear CYGTLS_PADSIZE. (_threadinfo::remove): Add debugging. (_threadinfo::find_tls): Ditto. * cygtls.h (_threadinfo::padding): Make zero length (for now?). * dcrt0.cc (dll_crt0_0): Move more initialization here from dll_crt0_1. (dll_crt0_1): See above. * dtable.h (dtable::lock): Remove commented out critical section locking. * dtable.h (dtable::init_lock): Remove commented out critical section locking. * dtable.h (dtable::unlock): Remove commented out critical section locking. * exceptions.cc (interruptible): bool'ize. * init.cc (threadfunc_fe): Revert to storing threadfunc at stack bottom. (munge_threadfunc): Ditto. Avoid adding overhead to calibration_thread. (prime_threads): Don't initialize tls stuff. (dll_entry): Make minor change to initialization order. * tlsoffsets.h: Regenerate. * sigproc.cc (wait_sig): Add sanity check for end of process thread exit. * select.h: Make minor formatting change. * Makefile.in: Add still more -fomit-frame-pointer functions. * dtable.h (dtable::lock): New function. (dtable::unlock): New function. (dtable::init_lock): New function. * cygheap.h (HEAP_TLS): Declare new enum value. (init_cygheap::threadlist): Declare new array. (init_cygheap::sthreads): Declare new variable. (cygheap_fdmanip::~cygheap_fdmanip): Use new dtable lock/unlock functions. (cygheap_fdnew::cygheap_fdnew): Ditto. (cygheap_fdget::cygheap_fdget): Ditto. * dtable.cc (dtable_init): Initialize fdtab critical section. (dtable::fixup_after_fork): Ditto. (dtable::fixup_after_exec): Ditto. (dtable::dup2): Use lock/unlock calls to protect access to fdtab. (dtable::find_fifo): Ditto. (dtable::fixup_before_fork): Ditto. (dtable::fixup_before_exec): Ditto. (dtable::set_file_pointers_for_exec): Ditto. (dtable::vfork_child_dup): Ditto. (dtable::vfork_parent_restore): Ditto. * syscalls.cc (close_all_files): Ditto. * sync.h (muto::acquired): Declare new function. (new_muto1): Declare new macro used to specify name of muto storage. * sync.cc (muto::acquired): Define new function. * cygthread.cc (cygthread::stub): Remove signal chain removal call since it is handled during initialization now. * cygthread.cc (cygthread::simplestub): Remove signal chain removal call since it is handled during initialization now. * cygtls.cc (sentry): New class used for locking. Use throughout. (_threadinfo::reset_exception): Don't pop stack. (_threadinfo::find_tls): Move from exceptions.cc. (_threadinfo::init_thread): Initialize array of threads rather than linked list. Take second argument indicating thread function for this thread. (_threadinfo::remove): Search thread array rather than linked list. Use sentry to lock. Only unlock if we got the lock. (_threadinfo::find_tls): Ditto for first two. (handle_threadlist_exception): Handle exceptions when manipulating the thread list in case of premature thread termination. (_threadinfo::init_threadlist_exceptions): Ditto. * cygtls.h (TLS_STACK_SIZE): Decrease size. (_threadinfo::padding): Add element to avoid overwriting lower part of stack. (_threadinfo::remove): Add a "wait" argument to control how long we wait for a lock before removing. * exceptions.cc (init_exception_handler): Make global. Take argument to control exception handler being set. (ctrl_c_handler): Wait forever when removing self from signal chain. (_threadinfo::find_tls): Move to cygtls.cc. (sig_handle): Reorganize detection for thread-specific signals. * heap.cc (heap_init): Rework slightly. Make fatal error more verbose. Remove malloc initialization since it can't happen during dll attach. * init.cc (search_for): Move address to search for on stack here. (threadfunc_ix): Ditto for stack offset. Make shared so that stack walk potentially only has to be done once when cygwin processes are running. (threadfunc_fe): Use standard tls to store thread function (may change back later). (calibration_thread): New function. Potentially called to find threadfunc_ix. (munge_threadfunc): Search for "search_for" value on stack. Output warning when thread func not found on stack. Use standard tls to store thread function. (prime_threads): New function. Called to prime thread front end. (dll_entry): Call dll_crt0_0 here when DLL_PROCESS_ATTACH. Call prime_threads here. Try to remove thread from signal list here. * sigproc.cc (wait_sig): Initialize threadlist exception stuff here. * thread.cc (pthread::exit): Pass argument to signal list remove function. * thread.h: Remove obsolete *ResourceLock defines. * tlsoffsets.h: Regenerate. * winsup.h (spf): Define temporary debug macro to be deleted later. * dcrt0.cc (dll_crt0_0): New function, called during DLL initialization. Mainly consists of code pulled from dll_crt0_1. (dll_crt0_1): See above. (_dll_crt0): Wait for initial calibration thread to complete, if appropriate. Move some stuff to dll_crt0_0. (initialize_main_tls): Accommodate argument change to _thread_info::init_thread. * fork.cc (fork_child): Ditto. (sync_with_child): Fix debug message. * external.cc (cygwin_internal): Remove special considerations for uninitialized dll since initialization happens during dll attach now. * dlfcn.cc (dlopen): Remove obsolete *ResourceLock calls. (dlclose): Ditto. * cygheap.h (init_cygheap::close_ctty): Declare new function. * cygheap.cc (init_cygheap::close_ctty): Define new function. * syscalls.cc (close_all_files): Use close_ctty. (setsid): Ditto. * cygthread.cc (cygthread::stub): Remove exception initialization. * cygthread.cc (cygthread::stub): Remove exception initialization. (cygthread::simplestub): Ditto. * thread.cc (pthread::thread_init_wrapper): Ditto. * cygtls.cc (_last_thread): Make static. (_threadinfo::call2): Initialize exception handler here. (_threadinfo::find_tls): Move here. * exceptions.cc (_threadinfo::find_tls): Move. * dcrt0.cc (__api_fatal): Add prefix info to message here rather than including it in every call to function. * winsup.h (api_fatal): Accommodate above change. * debug.cc (add_handle): Don't do anything if cygheap not around. (mark_closed): Ditto. * dll_init.cc (dll_list::detach): Fix debug output. * fork.cc (sync_with_child): Ditto. (vfork): Improve debug output. * heap.cc (heap_init): Ditto. * exceptions.cc (try_to_debug): Clarify message when debugger attaches.
803 lines
18 KiB
C++
803 lines
18 KiB
C++
/* pinfo.cc: process table support
|
|
|
|
Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003 Red Hat, Inc.
|
|
|
|
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. */
|
|
|
|
#include "winsup.h"
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <limits.h>
|
|
#include <stdarg.h>
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygerrno.h"
|
|
#include "sigproc.h"
|
|
#include "pinfo.h"
|
|
#include "cygwin_version.h"
|
|
#include "perprocess.h"
|
|
#include "environ.h"
|
|
#include <assert.h>
|
|
#include <ntdef.h>
|
|
#include "ntdll.h"
|
|
#include "cygthread.h"
|
|
#include "shared_info.h"
|
|
#include "cygheap.h"
|
|
#include "fhandler.h"
|
|
|
|
static char NO_COPY pinfo_dummy[sizeof (_pinfo)] = {0};
|
|
|
|
pinfo NO_COPY myself ((_pinfo *)&pinfo_dummy); // Avoid myself != NULL checks
|
|
|
|
HANDLE hexec_proc;
|
|
|
|
void __stdcall
|
|
pinfo_fixup_after_fork ()
|
|
{
|
|
if (hexec_proc)
|
|
CloseHandle (hexec_proc);
|
|
/* Keeps the cygpid from being reused. No rights required */
|
|
if (!DuplicateHandle (hMainProc, hMainProc, hMainProc, &hexec_proc, 0,
|
|
TRUE, 0))
|
|
{
|
|
system_printf ("couldn't save current process handle %p, %E", hMainProc);
|
|
hexec_proc = NULL;
|
|
}
|
|
}
|
|
|
|
/* Initialize the process table.
|
|
This is done once when the dll is first loaded. */
|
|
|
|
void __stdcall
|
|
set_myself (pid_t pid, HANDLE h)
|
|
{
|
|
DWORD winpid = GetCurrentProcessId ();
|
|
if (pid == 1)
|
|
pid = cygwin_pid (winpid);
|
|
myself.init (pid, PID_IN_USE | PID_MYSELF, h);
|
|
myself->dwProcessId = winpid;
|
|
myself->process_state |= PID_IN_USE;
|
|
myself->start_time = time (NULL); /* Register our starting time. */
|
|
|
|
(void) GetModuleFileName (NULL, myself->progname, sizeof (myself->progname));
|
|
if (!strace.active)
|
|
strace.hello ();
|
|
InitializeCriticalSection (&myself->lock);
|
|
return;
|
|
}
|
|
|
|
/* Initialize the process table entry for the current task.
|
|
This is not called for forked tasks, only execed ones. */
|
|
void __stdcall
|
|
pinfo_init (char **envp, int envc)
|
|
{
|
|
if (envp)
|
|
{
|
|
environ_init (envp, envc);
|
|
/* spawn has already set up a pid structure for us so we'll use that */
|
|
myself->process_state |= PID_CYGPARENT;
|
|
}
|
|
else
|
|
{
|
|
/* Invent our own pid. */
|
|
|
|
set_myself (1);
|
|
myself->ppid = 1;
|
|
myself->pgid = myself->sid = myself->pid;
|
|
myself->ctty = -1;
|
|
myself->uid = ILLEGAL_UID;
|
|
myself->gid = UNKNOWN_GID;
|
|
environ_init (NULL, 0); /* call after myself has been set up */
|
|
}
|
|
|
|
debug_printf ("pid %d, pgid %d", myself->pid, myself->pgid);
|
|
}
|
|
|
|
void
|
|
_pinfo::exit (UINT n, bool norecord)
|
|
{
|
|
if (this)
|
|
{
|
|
if (!norecord)
|
|
process_state = PID_EXITED;
|
|
|
|
/* FIXME: There is a potential race between an execed process and its
|
|
parent here. I hated to add a mutex just for this, though. */
|
|
struct rusage r;
|
|
fill_rusage (&r, hMainProc);
|
|
add_rusage (&rusage_self, &r);
|
|
}
|
|
|
|
cygthread::terminate ();
|
|
sigproc_printf ("Calling ExitProcess %d", n);
|
|
ExitProcess (n);
|
|
}
|
|
|
|
void
|
|
pinfo::init (pid_t n, DWORD flag, HANDLE in_h)
|
|
{
|
|
if (myself && n == myself->pid)
|
|
{
|
|
procinfo = myself;
|
|
destroy = 0;
|
|
h = NULL;
|
|
return;
|
|
}
|
|
|
|
void *mapaddr;
|
|
if (!(flag & PID_MYSELF))
|
|
mapaddr = NULL;
|
|
else
|
|
{
|
|
flag &= ~PID_MYSELF;
|
|
HANDLE hdummy;
|
|
mapaddr = open_shared (NULL, 0, hdummy, 0, SH_MYSELF);
|
|
}
|
|
|
|
int createit = flag & (PID_IN_USE | PID_EXECED);
|
|
DWORD access = FILE_MAP_READ
|
|
| (flag & (PID_IN_USE | PID_EXECED | PID_MAP_RW) ? FILE_MAP_WRITE : 0);
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
int created;
|
|
char mapname[CYG_MAX_PATH]; /* XXX Not a path */
|
|
shared_name (mapname, "cygpid", n);
|
|
|
|
int mapsize;
|
|
if (flag & PID_EXECED)
|
|
mapsize = PINFO_REDIR_SIZE;
|
|
else
|
|
mapsize = sizeof (_pinfo);
|
|
|
|
if (in_h)
|
|
{
|
|
h = in_h;
|
|
created = 0;
|
|
}
|
|
else if (!createit)
|
|
{
|
|
h = OpenFileMappingA (access, FALSE, mapname);
|
|
created = 0;
|
|
}
|
|
else
|
|
{
|
|
char sa_buf[1024];
|
|
PSECURITY_ATTRIBUTES sec_attribs =
|
|
sec_user_nih (sa_buf, cygheap->user.sid(), well_known_world_sid,
|
|
FILE_MAP_READ);
|
|
h = CreateFileMapping (INVALID_HANDLE_VALUE, sec_attribs,
|
|
PAGE_READWRITE, 0, mapsize, mapname);
|
|
created = h && GetLastError () != ERROR_ALREADY_EXISTS;
|
|
}
|
|
|
|
if (!h)
|
|
{
|
|
if (createit)
|
|
__seterrno ();
|
|
procinfo = NULL;
|
|
return;
|
|
}
|
|
|
|
procinfo = (_pinfo *) MapViewOfFileEx (h, access, 0, 0, 0, mapaddr);
|
|
ProtectHandle1 (h, pinfo_shared_handle);
|
|
|
|
if ((procinfo->process_state & PID_INITIALIZING) && (flag & PID_NOREDIR)
|
|
&& cygwin_pid (procinfo->dwProcessId) != procinfo->pid)
|
|
{
|
|
release ();
|
|
set_errno (ENOENT);
|
|
return;
|
|
}
|
|
|
|
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 ();
|
|
if (flag & PID_ALLPIDS)
|
|
{
|
|
set_errno (ENOENT);
|
|
break;
|
|
}
|
|
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 && createit && (procinfo->process_state & PID_EXITED))
|
|
{
|
|
low_priority_sleep (5);
|
|
release ();
|
|
continue;
|
|
}
|
|
|
|
if (!created)
|
|
/* nothing */;
|
|
else if (!(flag & PID_EXECED))
|
|
procinfo->pid = n;
|
|
else
|
|
{
|
|
procinfo->process_state |= PID_IN_USE | PID_EXECED;
|
|
procinfo->pid = myself->pid;
|
|
}
|
|
break;
|
|
}
|
|
destroy = 1;
|
|
}
|
|
|
|
void
|
|
pinfo::set_acl()
|
|
{
|
|
char sa_buf[1024];
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
sec_acl ((PACL) sa_buf, true, true, cygheap->user.sid (),
|
|
well_known_world_sid, FILE_MAP_READ);
|
|
if (!InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION))
|
|
debug_printf ("InitializeSecurityDescriptor %E");
|
|
else if (!SetSecurityDescriptorDacl (&sd, TRUE, (PACL) sa_buf, FALSE))
|
|
debug_printf ("SetSecurityDescriptorDacl %E");
|
|
else if (!SetKernelObjectSecurity (h, DACL_SECURITY_INFORMATION, &sd))
|
|
debug_printf ("SetKernelObjectSecurity %E");
|
|
}
|
|
|
|
void
|
|
_pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch)
|
|
{
|
|
debug_printf ("checking if /dev/tty%d differs from input", ctty);
|
|
if ((ctty < 0 || ctty == tc->ntty) && !(flags & O_NOCTTY))
|
|
{
|
|
ctty = tc->ntty;
|
|
syscall_printf ("attached tty%d sid %d, pid %d, tty->pgid %d, tty->sid %d",
|
|
tc->ntty, sid, pid, pgid, tc->getsid ());
|
|
|
|
pinfo p (tc->getsid ());
|
|
if (sid == pid && (!p || p->pid == pid || !proc_exists (p)))
|
|
{
|
|
paranoid_printf ("resetting tty%d sid. Was %d, now %d. pgid was %d, now %d.",
|
|
tc->ntty, tc->getsid (), sid, tc->getpgid (), pgid);
|
|
/* We are the session leader */
|
|
tc->setsid (sid);
|
|
tc->setpgid (pgid);
|
|
}
|
|
else
|
|
sid = tc->getsid ();
|
|
if (tc->getpgid () == 0)
|
|
tc->setpgid (pgid);
|
|
if (cygheap->ctty != arch)
|
|
{
|
|
debug_printf ("cygheap->ctty %p, arch %p", cygheap->ctty, arch);
|
|
if (!cygheap->ctty)
|
|
syscall_printf ("ctty NULL");
|
|
else
|
|
{
|
|
syscall_printf ("ctty %p, usecount %d", cygheap->ctty,
|
|
cygheap->ctty->usecount);
|
|
cygheap->ctty->close ();
|
|
}
|
|
cygheap->ctty = arch;
|
|
if (arch)
|
|
{
|
|
arch->usecount++;
|
|
cygheap->open_fhs++;
|
|
report_tty_counts (cygheap->ctty, "ctty", "incremented ", "");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool
|
|
_pinfo::alive ()
|
|
{
|
|
HANDLE h = OpenProcess (PROCESS_QUERY_INFORMATION, false, dwProcessId);
|
|
if (h)
|
|
CloseHandle (h);
|
|
return !!h;
|
|
}
|
|
|
|
extern char **__argv;
|
|
|
|
void
|
|
_pinfo::commune_recv ()
|
|
{
|
|
DWORD nr;
|
|
DWORD code;
|
|
HANDLE hp;
|
|
HANDLE __fromthem = NULL;
|
|
HANDLE __tothem = NULL;
|
|
|
|
hp = OpenProcess (PROCESS_DUP_HANDLE, false, dwProcessId);
|
|
if (!hp)
|
|
{
|
|
sigproc_printf ("couldn't open handle for pid %d(%u)", pid, dwProcessId);
|
|
hello_pid = -1;
|
|
return;
|
|
}
|
|
if (!DuplicateHandle (hp, fromthem, hMainProc, &__fromthem, 0, false, DUPLICATE_SAME_ACCESS))
|
|
{
|
|
sigproc_printf ("couldn't duplicate fromthem, %E");
|
|
CloseHandle (hp);
|
|
hello_pid = -1;
|
|
return;
|
|
}
|
|
|
|
if (!DuplicateHandle (hp, tothem, hMainProc, &__tothem, 0, false, DUPLICATE_SAME_ACCESS))
|
|
{
|
|
sigproc_printf ("couldn't duplicate tothem, %E");
|
|
CloseHandle (__fromthem);
|
|
CloseHandle (hp);
|
|
hello_pid = -1;
|
|
return;
|
|
}
|
|
|
|
CloseHandle (hp);
|
|
hello_pid = 0;
|
|
|
|
if (!ReadFile (__fromthem, &code, sizeof code, &nr, NULL) || nr != sizeof code)
|
|
{
|
|
/* __seterrno ();*/ // this is run from the signal thread, so don't set errno
|
|
goto out;
|
|
}
|
|
|
|
switch (code)
|
|
{
|
|
case PICOM_CMDLINE:
|
|
{
|
|
unsigned n = 1;
|
|
CloseHandle (__fromthem); __fromthem = NULL;
|
|
extern int __argc_safe;
|
|
const char *argv[__argc_safe + 1];
|
|
for (int i = 0; i < __argc_safe; i++)
|
|
{
|
|
if (IsBadStringPtr (__argv[i], INT32_MAX))
|
|
argv[i] = "";
|
|
else
|
|
argv[i] = __argv[i];
|
|
n += strlen (argv[i]) + 1;
|
|
}
|
|
argv[__argc_safe] = NULL;
|
|
if (!WriteFile (__tothem, &n, sizeof n, &nr, NULL))
|
|
{
|
|
/*__seterrno ();*/ // this is run from the signal thread, so don't set errno
|
|
sigproc_printf ("WriteFile sizeof argv failed, %E");
|
|
}
|
|
else
|
|
for (const char **a = argv; *a; a++)
|
|
if (!WriteFile (__tothem, *a, strlen (*a) + 1, &nr, NULL))
|
|
{
|
|
sigproc_printf ("WriteFile arg %d failed, %E", a - argv);
|
|
break;
|
|
}
|
|
if (!WriteFile (__tothem, "", 1, &nr, NULL))
|
|
{
|
|
sigproc_printf ("WriteFile null failed, %E");
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
case PICOM_FIFO:
|
|
{
|
|
int formic;
|
|
if (!ReadFile (__fromthem, &formic, sizeof formic, &nr, NULL)
|
|
|| nr != sizeof formic)
|
|
{
|
|
/* __seterrno ();*/ // this is run from the signal thread, so don't set errno
|
|
goto out;
|
|
}
|
|
|
|
fhandler_fifo *fh = cygheap->fdtab.find_fifo ((ATOM) formic);
|
|
HANDLE it[] = {(fh->get_handle ()), (fh->get_output_handle ())};
|
|
|
|
if (!WriteFile (__tothem, it, sizeof (it), &nr, NULL))
|
|
{
|
|
/*__seterrno ();*/ // this is run from the signal thread, so don't set errno
|
|
sigproc_printf ("WriteFile read handle failed, %E");
|
|
}
|
|
|
|
(void) ReadFile (__fromthem, &nr, sizeof (nr), &nr, NULL);
|
|
break;
|
|
}
|
|
}
|
|
|
|
out:
|
|
if (__fromthem)
|
|
CloseHandle (__fromthem);
|
|
if (__tothem)
|
|
CloseHandle (__tothem);
|
|
}
|
|
|
|
#define PIPEBUFSIZE (16 * sizeof (DWORD))
|
|
|
|
commune_result
|
|
_pinfo::commune_send (DWORD code, ...)
|
|
{
|
|
HANDLE fromthem = NULL, tome = NULL;
|
|
HANDLE fromme = NULL, tothem = NULL;
|
|
DWORD nr;
|
|
commune_result res;
|
|
va_list args;
|
|
|
|
va_start (args, code);
|
|
|
|
res.s = NULL;
|
|
res.n = 0;
|
|
|
|
if (!this || !pid)
|
|
{
|
|
set_errno (ESRCH);
|
|
goto err;
|
|
}
|
|
if (!CreatePipe (&fromthem, &tome, &sec_all_nih, PIPEBUFSIZE))
|
|
{
|
|
sigproc_printf ("first CreatePipe failed, %E");
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
if (!CreatePipe (&fromme, &tothem, &sec_all_nih, PIPEBUFSIZE))
|
|
{
|
|
sigproc_printf ("first CreatePipe failed, %E");
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
EnterCriticalSection (&myself->lock);
|
|
myself->tothem = tome;
|
|
myself->fromthem = fromme;
|
|
myself->hello_pid = pid;
|
|
if (!WriteFile (tothem, &code, sizeof code, &nr, NULL) || nr != sizeof code)
|
|
{
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
|
|
switch (code)
|
|
{
|
|
case PICOM_FIFO:
|
|
{
|
|
int formic = va_arg (args, int);
|
|
if (!WriteFile (tothem, &formic, sizeof formic, &nr, NULL) || nr != sizeof formic)
|
|
{
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sig_send (this, __SIGCOMMUNE))
|
|
goto err;
|
|
|
|
/* FIXME: Need something better than an busy loop here */
|
|
bool isalive;
|
|
for (int i = 0; (isalive = alive ()) && (i < 10000); i++)
|
|
if (myself->hello_pid <= 0)
|
|
break;
|
|
else
|
|
low_priority_sleep (0);
|
|
|
|
CloseHandle (tome);
|
|
tome = NULL;
|
|
CloseHandle (fromme);
|
|
fromme = NULL;
|
|
|
|
if (!isalive)
|
|
{
|
|
set_errno (ESRCH);
|
|
goto err;
|
|
}
|
|
|
|
if (myself->hello_pid < 0)
|
|
{
|
|
set_errno (ENOSYS);
|
|
goto err;
|
|
}
|
|
|
|
size_t n;
|
|
switch (code)
|
|
{
|
|
case PICOM_CMDLINE:
|
|
if (!ReadFile (fromthem, &n, sizeof n, &nr, NULL) || nr != sizeof n)
|
|
{
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
res.s = (char *) malloc (n);
|
|
char *p;
|
|
for (p = res.s; ReadFile (fromthem, p, n, &nr, NULL); p += nr)
|
|
continue;
|
|
if ((unsigned) (p - res.s) != n)
|
|
{
|
|
__seterrno ();
|
|
goto err;
|
|
}
|
|
res.n = n;
|
|
break;
|
|
case PICOM_FIFO:
|
|
{
|
|
DWORD x = ReadFile (fromthem, res.handles, sizeof (res.handles), &nr, NULL);
|
|
WriteFile (tothem, &x, sizeof (x), &x, NULL);
|
|
if (!x)
|
|
goto err;
|
|
if (nr != sizeof (res.handles))
|
|
{
|
|
set_errno (EPIPE);
|
|
goto err;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
CloseHandle (tothem);
|
|
CloseHandle (fromthem);
|
|
goto out;
|
|
|
|
err:
|
|
if (tome)
|
|
CloseHandle (tome);
|
|
if (fromthem)
|
|
CloseHandle (fromthem);
|
|
if (tothem)
|
|
CloseHandle (tothem);
|
|
if (fromme)
|
|
CloseHandle (fromme);
|
|
memset (&res, 0, sizeof (res));
|
|
|
|
out:
|
|
myself->hello_pid = 0;
|
|
LeaveCriticalSection (&myself->lock);
|
|
return res;
|
|
}
|
|
|
|
char *
|
|
_pinfo::cmdline (size_t& n)
|
|
{
|
|
char *s;
|
|
if (!this || !pid)
|
|
return NULL;
|
|
if (pid != myself->pid)
|
|
{
|
|
commune_result cr = commune_send (PICOM_CMDLINE);
|
|
s = cr.s;
|
|
n = cr.n;
|
|
}
|
|
else
|
|
{
|
|
n = 1;
|
|
for (char **a = __argv; *a; a++)
|
|
n += strlen (*a) + 1;
|
|
char *p;
|
|
p = s = (char *) malloc (n);
|
|
for (char **a = __argv; *a; a++)
|
|
{
|
|
strcpy (p, *a);
|
|
p = strchr (p, '\0') + 1;
|
|
}
|
|
*p = '\0';
|
|
}
|
|
return s;
|
|
}
|
|
|
|
void
|
|
pinfo::release ()
|
|
{
|
|
if (h)
|
|
{
|
|
#ifdef DEBUGGING
|
|
if (((DWORD) procinfo & 0x77000000) == 0x61000000)
|
|
try_to_debug ();
|
|
#endif
|
|
UnmapViewOfFile (procinfo);
|
|
procinfo = NULL;
|
|
ForceCloseHandle1 (h, pinfo_shared_handle);
|
|
h = NULL;
|
|
}
|
|
}
|
|
|
|
/* DOCTOOL-START
|
|
|
|
<sect1 id="func-cygwin-winpid-to-pid">
|
|
<title>cygwin_winpid_to_pid</title>
|
|
|
|
<funcsynopsis>
|
|
<funcdef>extern "C" pid_t
|
|
<function>cygwin_winpid_to_pid</function>
|
|
</funcdef>
|
|
<paramdef>int <parameter>winpid</parameter></paramdef>
|
|
</funcsynopsis>
|
|
|
|
<para>Given a windows pid, converts to the corresponding Cygwin
|
|
pid, if any. Returns -1 if windows pid does not correspond to
|
|
a cygwin pid.</para>
|
|
<example>
|
|
<title>Example use of cygwin_winpid_to_pid</title>
|
|
<programlisting>
|
|
extern "C" cygwin_winpid_to_pid (int winpid);
|
|
pid_t mypid;
|
|
mypid = cygwin_winpid_to_pid (windows_pid);
|
|
</programlisting>
|
|
</example>
|
|
</sect1>
|
|
|
|
DOCTOOL-END */
|
|
|
|
extern "C" pid_t
|
|
cygwin_winpid_to_pid (int winpid)
|
|
{
|
|
pinfo p (cygwin_pid (winpid));
|
|
if (p)
|
|
return p->pid;
|
|
|
|
set_errno (ESRCH);
|
|
return (pid_t) -1;
|
|
}
|
|
|
|
#include <tlhelp32.h>
|
|
|
|
#define slop_pidlist 200
|
|
#define size_pidlist(i) (sizeof (pidlist[0]) * ((i) + 1))
|
|
#define size_pinfolist(i) (sizeof (pinfolist[0]) * ((i) + 1))
|
|
|
|
inline void
|
|
winpids::add (DWORD& nelem, bool winpid, DWORD pid)
|
|
{
|
|
pid_t cygpid = cygwin_pid (pid);
|
|
if (nelem >= npidlist)
|
|
{
|
|
npidlist += slop_pidlist;
|
|
pidlist = (DWORD *) realloc (pidlist, size_pidlist (npidlist + 1));
|
|
pinfolist = (pinfo *) realloc (pinfolist, size_pinfolist (npidlist + 1));
|
|
}
|
|
|
|
pinfolist[nelem].init (cygpid, PID_NOREDIR | (winpid ? PID_ALLPIDS : 0)
|
|
| pinfo_access);
|
|
if (winpid)
|
|
goto out;
|
|
|
|
if (!pinfolist[nelem])
|
|
{
|
|
if (!pinfo_access)
|
|
return;
|
|
pinfolist[nelem].init (cygpid, PID_NOREDIR | (winpid ? PID_ALLPIDS : 0));
|
|
if (!pinfolist[nelem])
|
|
return;
|
|
}
|
|
|
|
/* Scan list of previously recorded pids to make sure that this pid hasn't
|
|
shown up before. This can happen when a process execs. */
|
|
for (unsigned i = 0; i < nelem; i++)
|
|
if (pinfolist[i]->pid == pinfolist[nelem]->pid)
|
|
{
|
|
if ((_pinfo *) pinfolist[nelem] != (_pinfo *) myself)
|
|
pinfolist[nelem].release ();
|
|
return;
|
|
}
|
|
|
|
out:
|
|
pidlist[nelem++] = pid;
|
|
}
|
|
|
|
DWORD
|
|
winpids::enumNT (bool winpid)
|
|
{
|
|
static DWORD szprocs;
|
|
static SYSTEM_PROCESSES *procs;
|
|
|
|
DWORD nelem = 0;
|
|
if (!szprocs)
|
|
procs = (SYSTEM_PROCESSES *) malloc (sizeof (*procs) + (szprocs = 200 * sizeof (*procs)));
|
|
|
|
NTSTATUS res;
|
|
for (;;)
|
|
{
|
|
res = NtQuerySystemInformation (SystemProcessesAndThreadsInformation,
|
|
procs, szprocs, NULL);
|
|
if (res == 0)
|
|
break;
|
|
|
|
if (res == STATUS_INFO_LENGTH_MISMATCH)
|
|
procs = (SYSTEM_PROCESSES *) realloc (procs, szprocs += 200 * sizeof (*procs));
|
|
else
|
|
{
|
|
system_printf ("error %p reading system process information", res);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
SYSTEM_PROCESSES *px = procs;
|
|
for (;;)
|
|
{
|
|
if (px->ProcessId)
|
|
add (nelem, winpid, px->ProcessId);
|
|
if (!px->NextEntryDelta)
|
|
break;
|
|
px = (SYSTEM_PROCESSES *) ((char *) px + px->NextEntryDelta);
|
|
}
|
|
|
|
return nelem;
|
|
}
|
|
|
|
DWORD
|
|
winpids::enum9x (bool winpid)
|
|
{
|
|
DWORD nelem = 0;
|
|
|
|
HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
|
|
if (!h)
|
|
{
|
|
system_printf ("Couldn't create process snapshot, %E");
|
|
return 0;
|
|
}
|
|
|
|
PROCESSENTRY32 proc;
|
|
proc.dwSize = sizeof (proc);
|
|
|
|
if (Process32First (h, &proc))
|
|
do
|
|
{
|
|
if (proc.th32ProcessID)
|
|
add (nelem, winpid, proc.th32ProcessID);
|
|
}
|
|
while (Process32Next (h, &proc));
|
|
|
|
CloseHandle (h);
|
|
return nelem;
|
|
}
|
|
|
|
NO_COPY CRITICAL_SECTION winpids::cs;
|
|
|
|
void
|
|
winpids::set (bool winpid)
|
|
{
|
|
EnterCriticalSection (&cs);
|
|
npids = (this->*enum_processes) (winpid);
|
|
if (pidlist)
|
|
pidlist[npids] = 0;
|
|
LeaveCriticalSection (&cs);
|
|
}
|
|
|
|
void
|
|
winpids::init ()
|
|
{
|
|
InitializeCriticalSection (&cs);
|
|
}
|
|
|
|
DWORD
|
|
winpids::enum_init (bool winpid)
|
|
{
|
|
if (wincap.is_winnt ())
|
|
enum_processes = &winpids::enumNT;
|
|
else
|
|
enum_processes = &winpids::enum9x;
|
|
|
|
return (this->*enum_processes) (winpid);
|
|
}
|
|
|
|
void
|
|
winpids::release ()
|
|
{
|
|
for (unsigned i = 0; i < npids; i++)
|
|
if (pinfolist[i] && (_pinfo *) pinfolist[i] != (_pinfo *) myself)
|
|
pinfolist[i].release ();
|
|
}
|
|
|
|
winpids::~winpids ()
|
|
{
|
|
if (npidlist)
|
|
{
|
|
release ();
|
|
free (pidlist);
|
|
free (pinfolist);
|
|
}
|
|
}
|