2000-02-18 03:38:33 +08:00
|
|
|
/* pinfo.cc: process table support
|
|
|
|
|
|
|
|
Copyright 1996, 1997, 1998, 2000 Cygnus Solutions.
|
|
|
|
|
|
|
|
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 <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "thread.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static char NO_COPY pinfo_dummy[sizeof(pinfo)] = {0};
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
pinfo NO_COPY myself ((_pinfo *)&pinfo_dummy); // Avoid myself != NULL checks
|
|
|
|
static pinfo NO_COPY myself_identity ((_pinfo *)&pinfo_dummy);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Initialize the process table.
|
|
|
|
This is done once when the dll is first loaded. */
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
void __stdcall
|
2000-08-24 12:07:50 +08:00
|
|
|
set_myself (pid_t pid, HANDLE h)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
DWORD winpid = GetCurrentProcessId ();
|
|
|
|
if (pid == 1)
|
|
|
|
pid = cygwin_pid (winpid);
|
2000-08-24 12:07:50 +08:00
|
|
|
myself.init (pid, 1, h);
|
2000-07-30 00:24:59 +08:00
|
|
|
myself->dwProcessId = winpid;
|
|
|
|
myself->process_state |= PID_IN_USE;
|
2000-02-18 03:38:33 +08:00
|
|
|
myself->start_time = time (NULL); /* Register our starting time. */
|
2000-07-30 00:24:59 +08:00
|
|
|
pid_t myself_cyg_pid = cygwin_pid (myself->dwProcessId);
|
2000-08-24 12:07:50 +08:00
|
|
|
if (pid != myself_cyg_pid && parent_alive)
|
2000-07-30 00:24:59 +08:00
|
|
|
myself_identity.init (myself_cyg_pid, PID_EXECED);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
char buf[30];
|
2000-03-07 13:33:09 +08:00
|
|
|
__small_sprintf (buf, "cYg%8x %x", _STRACE_INTERFACE_ACTIVATE_ADDR,
|
|
|
|
&strace.active);
|
2000-02-18 03:38:33 +08:00
|
|
|
OutputDebugString (buf);
|
2000-02-28 13:05:33 +08:00
|
|
|
|
|
|
|
(void) GetModuleFileName (NULL, myself->progname,
|
|
|
|
sizeof(myself->progname));
|
2000-03-07 13:33:09 +08:00
|
|
|
if (strace.active)
|
2000-02-28 13:05:33 +08:00
|
|
|
{
|
|
|
|
extern char osname[];
|
2000-08-03 03:26:01 +08:00
|
|
|
strace.prntf (1, NULL, "**********************************************");
|
|
|
|
strace.prntf (1, NULL, "Program name: %s", myself->progname);
|
|
|
|
strace.prntf (1, NULL, "App version: %d.%d, api: %d.%d",
|
|
|
|
user_data->dll_major, user_data->dll_minor,
|
|
|
|
user_data->api_major, user_data->api_minor);
|
|
|
|
strace.prntf (1, NULL, "DLL version: %d.%d, api: %d.%d",
|
|
|
|
cygwin_version.dll_major, cygwin_version.dll_minor,
|
|
|
|
cygwin_version.api_major, cygwin_version.api_minor);
|
|
|
|
strace.prntf (1, NULL, "DLL build: %s", cygwin_version.dll_build_date);
|
|
|
|
strace.prntf (1, NULL, "OS version: Windows %s", osname);
|
|
|
|
strace.prntf (1, NULL, "**********************************************");
|
2000-02-28 13:05:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the process table entry for the current task.
|
|
|
|
This is not called for fork'd tasks, only exec'd ones. */
|
|
|
|
void __stdcall
|
|
|
|
pinfo_init (LPBYTE info)
|
|
|
|
{
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
/* The process was execed. Reuse entry from the original
|
|
|
|
owner of this pid. */
|
2000-04-26 13:13:32 +08:00
|
|
|
environ_init (0); /* Needs myself but affects calls below */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* spawn has already set up a pid structure for us so we'll use that */
|
|
|
|
|
|
|
|
myself->process_state |= PID_CYGPARENT;
|
|
|
|
|
|
|
|
/* Inherit file descriptor information from parent in info.
|
|
|
|
*/
|
2000-08-12 12:48:44 +08:00
|
|
|
LPBYTE b = fdtab.de_linearize_fd_array (info);
|
2000-02-18 03:38:33 +08:00
|
|
|
extern char title_buf[];
|
|
|
|
if (b && *b)
|
|
|
|
old_title = strcpy (title_buf, (char *)b);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Invent our own pid. */
|
|
|
|
|
2000-08-24 12:07:50 +08:00
|
|
|
set_myself (1, NULL);
|
2000-07-30 00:24:59 +08:00
|
|
|
myself->ppid = 1;
|
|
|
|
myself->pgid = myself->sid = myself->pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
myself->ctty = -1;
|
|
|
|
myself->uid = USHRT_MAX;
|
|
|
|
|
2000-04-26 13:13:32 +08:00
|
|
|
environ_init (0); /* call after myself has been set up */
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf ("pid %d, pgid %d", myself->pid, myself->pgid);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sigaction&
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::getsig(int sig)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
if ( thread2signal )
|
|
|
|
return thread2signal->sigs[sig];
|
|
|
|
return sigs[sig];
|
|
|
|
#else
|
|
|
|
return sigs[sig];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
sigset_t&
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::getsigmask ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
if ( thread2signal )
|
|
|
|
return *thread2signal->sigmask;
|
|
|
|
return sig_mask;
|
|
|
|
#else
|
|
|
|
return sig_mask;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::setsigmask (sigset_t _mask)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
if ( thread2signal )
|
|
|
|
*(thread2signal->sigmask) = _mask;
|
|
|
|
sig_mask=_mask;
|
|
|
|
#else
|
|
|
|
sig_mask=_mask;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
LONG *
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::getsigtodo(int sig)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
if ( thread2signal )
|
|
|
|
return thread2signal->sigtodo + __SIGOFFSET + sig;
|
|
|
|
return _sigtodo + __SIGOFFSET + sig;
|
|
|
|
#else
|
|
|
|
return _sigtodo + __SIGOFFSET + sig;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
extern HANDLE hMainThread;
|
|
|
|
|
|
|
|
HANDLE
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::getthread2signal()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
if ( thread2signal )
|
|
|
|
return thread2signal->win32_obj_id;
|
|
|
|
return hMainThread;
|
|
|
|
#else
|
|
|
|
return hMainThread;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::setthread2signal(void *_thr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
// assert has myself lock
|
|
|
|
thread2signal=(ThreadItem*)_thr;
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-30 00:24:59 +08:00
|
|
|
_pinfo::copysigs(_pinfo *_other)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
sigs = _other->sigs;
|
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
void
|
|
|
|
_pinfo::record_death ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
/* CGF FIXME - needed? */
|
|
|
|
if (dwProcessId == GetCurrentProcessId () && !my_parent_is_alive ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
process_state = PID_NOT_IN_USE;
|
|
|
|
hProcess = NULL;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-24 12:07:50 +08:00
|
|
|
pinfo::init (pid_t n, DWORD create, HANDLE in_h)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
if (n == myself->pid)
|
|
|
|
{
|
|
|
|
child = myself;
|
|
|
|
destroy = 0;
|
|
|
|
h = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
int created;
|
|
|
|
char mapname[MAX_PATH];
|
|
|
|
__small_sprintf (mapname, "cygpid.%x", n);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
int mapsize;
|
|
|
|
if (create & PID_EXECED)
|
|
|
|
mapsize = PINFO_REDIR_SIZE;
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2000-07-30 00:24:59 +08:00
|
|
|
mapsize = sizeof (_pinfo);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-24 12:07:50 +08:00
|
|
|
if (in_h)
|
|
|
|
{
|
|
|
|
h = in_h;
|
|
|
|
created = 0;
|
|
|
|
}
|
|
|
|
else if (!create)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
/* CGF FIXME -- deal with inheritance after an exec */
|
|
|
|
h = OpenFileMappingA (FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapname);
|
|
|
|
created = 0;
|
|
|
|
}
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-08-24 12:07:50 +08:00
|
|
|
h = CreateFileMapping ((HANDLE) 0xffffffff, &sec_all_nih,
|
2000-07-30 00:24:59 +08:00
|
|
|
PAGE_READWRITE, 0, mapsize, mapname);
|
|
|
|
created = h && GetLastError () != ERROR_ALREADY_EXISTS;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
if (!h)
|
|
|
|
{
|
|
|
|
if (create)
|
|
|
|
__seterrno ();
|
|
|
|
child = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-24 12:07:50 +08:00
|
|
|
ProtectHandle1 (h, pinfo_shared_handle);
|
2000-07-30 00:24:59 +08:00
|
|
|
child = (_pinfo *) MapViewOfFile (h, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
if (child->process_state & PID_EXECED)
|
|
|
|
{
|
|
|
|
pid_t realpid = child->pid;
|
2000-08-24 12:07:50 +08:00
|
|
|
debug_printf ("execed process windows pid %d, cygwin pid %d", n, realpid);
|
2000-07-30 00:24:59 +08:00
|
|
|
release ();
|
|
|
|
if (realpid == n)
|
|
|
|
api_fatal ("retrieval of execed process info for pid %d failed due to recursion.", n);
|
|
|
|
return init (realpid);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
if (created)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
if (!(create & PID_EXECED))
|
|
|
|
child->pid = n;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child->pid = myself->pid;
|
|
|
|
child->process_state |= PID_IN_USE | PID_EXECED;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
destroy = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2000-07-30 00:24:59 +08:00
|
|
|
pinfo p (winpid);
|
|
|
|
if (p)
|
|
|
|
return p->pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
set_errno (ESRCH);
|
|
|
|
return (pid_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <tlhelp32.h>
|
|
|
|
#include <psapi.h>
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI * ENUMPROCESSES) (DWORD *, DWORD, DWORD *);
|
|
|
|
typedef HANDLE (WINAPI * CREATESNAPSHOT) (DWORD, DWORD);
|
|
|
|
typedef BOOL (WINAPI * PROCESSWALK) (HANDLE, LPPROCESSENTRY32);
|
|
|
|
typedef BOOL (WINAPI * CLOSESNAPSHOT) (HANDLE);
|
|
|
|
|
|
|
|
static NO_COPY CREATESNAPSHOT myCreateToolhelp32Snapshot = NULL;
|
|
|
|
static NO_COPY PROCESSWALK myProcess32First = NULL;
|
|
|
|
static NO_COPY PROCESSWALK myProcess32Next = NULL;
|
|
|
|
static BOOL WINAPI enum_init (DWORD *lpidProcess, DWORD cb, DWORD *cbneeded);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
static NO_COPY ENUMPROCESSES myEnumProcesses = enum_init;
|
|
|
|
|
|
|
|
static BOOL WINAPI
|
|
|
|
EnumProcessesW95 (DWORD *lpidProcess, DWORD cb, DWORD *cbneeded)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
|
|
|
|
*cbneeded = 0;
|
|
|
|
h = myCreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (!h)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PROCESSENTRY32 proc;
|
|
|
|
int i = 0;
|
|
|
|
proc.dwSize = sizeof (proc);
|
|
|
|
if (myProcess32First(h, &proc))
|
|
|
|
do
|
|
|
|
lpidProcess[i++] = cygwin_pid (proc.th32ProcessID);
|
|
|
|
while (myProcess32Next (h, &proc));
|
|
|
|
CloseHandle (h);
|
|
|
|
if (i == 0)
|
|
|
|
return 0;
|
|
|
|
*cbneeded = i * sizeof (DWORD);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winpids::init ()
|
|
|
|
{
|
|
|
|
DWORD n;
|
|
|
|
if (!myEnumProcesses (pidlist, sizeof (pidlist) / sizeof (pidlist[0]), &n))
|
|
|
|
npids = 0;
|
|
|
|
else
|
|
|
|
npids = n / sizeof (pidlist[0]);
|
|
|
|
|
|
|
|
pidlist[npids] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI
|
|
|
|
enum_init (DWORD *lpidProcess, DWORD cb, DWORD *cbneeded)
|
|
|
|
{
|
|
|
|
HINSTANCE h;
|
|
|
|
if (os_being_run == winNT)
|
|
|
|
{
|
|
|
|
h = LoadLibrary ("psapi.dll");
|
|
|
|
if (!h)
|
|
|
|
return 0;
|
|
|
|
myEnumProcesses = (ENUMPROCESSES) GetProcAddress (h, "EnumProcesses");
|
|
|
|
if (!myEnumProcesses)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h = GetModuleHandle("kernel32.dll");
|
|
|
|
myCreateToolhelp32Snapshot = (CREATESNAPSHOT)
|
|
|
|
GetProcAddress(h, "CreateToolhelp32Snapshot");
|
|
|
|
myProcess32First = (PROCESSWALK)
|
|
|
|
GetProcAddress(h, "Process32First");
|
|
|
|
myProcess32Next = (PROCESSWALK)
|
|
|
|
GetProcAddress(h, "Process32Next");
|
|
|
|
if (!myCreateToolhelp32Snapshot || !myProcess32First || !myProcess32Next)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
myEnumProcesses = EnumProcessesW95;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
return myEnumProcesses (lpidProcess, cb, cbneeded);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|