From 448cf5aa4b429d5a9cebf92a0da4ab4b5b6d23fe Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Sat, 2 Feb 2019 12:23:39 +0100 Subject: [PATCH] Cygwin: processes: fix handling of native Windows processes Since commit b5e1003722cb14235c4f166be72c09acdffc62ea, native Windows processes not started by Cygwin processes don't have a Cygwin PID anymore. This breaks ps -W and kill -f . Introduce MAX_PID (65536 for now). Cygwin processes as well as native Windows processes started from a Cygwin process get a PID < MAX_PID. All other native Windows processes get a faked Cygwin PID >= MAX_PID. Signed-off-by: Corinna Vinschen --- winsup/cygwin/external.cc | 12 ++++++++---- winsup/cygwin/pinfo.cc | 2 +- winsup/cygwin/pinfo.h | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/winsup/cygwin/external.cc b/winsup/cygwin/external.cc index 3a9130efd..ae30695a1 100644 --- a/winsup/cygwin/external.cc +++ b/winsup/cygwin/external.cc @@ -63,19 +63,23 @@ fillout_pinfo (pid_t pid, int winpid) _pinfo *p = pids[i]; i++; + /* Native Windows process not started from Cygwin have no procinfo + attached. They don't have a real Cygwin PID either. We fake a + Cygwin PID beyond MAX_PID. */ if (!p) { - if (!nextpid && thispid != (DWORD) pid) + if (!nextpid && thispid + MAX_PID != (DWORD) pid) continue; - ep.pid = cygwin_pid (thispid); + ep.pid = thispid + MAX_PID; ep.dwProcessId = thispid; ep.process_state = PID_IN_USE; ep.ctty = -1; break; } - else if (nextpid || p->pid == pid || (winpid && thispid == (DWORD) pid)) + else if (nextpid || p->pid == pid) { - ep.ctty = (p->ctty < 0 || iscons_dev (p->ctty)) ? p->ctty : device::minor (p->ctty); + ep.ctty = (p->ctty < 0 || iscons_dev (p->ctty)) + ? p->ctty : device::minor (p->ctty); ep.pid = p->pid; ep.ppid = p->ppid; ep.dwProcessId = p->dwProcessId; diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 98168c76a..c9025774f 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -241,7 +241,7 @@ create_cygwin_pid () do { pid = ((uint32_t) InterlockedIncrement (&cygwin_shared->pid_src)) - % 65536; + % MAX_PID; } while (pid < 2); __small_swprintf (sym_name, L"cygpid.%u", pid); diff --git a/winsup/cygwin/pinfo.h b/winsup/cygwin/pinfo.h index 81e10d3d5..a1e5afe23 100644 --- a/winsup/cygwin/pinfo.h +++ b/winsup/cygwin/pinfo.h @@ -211,6 +211,8 @@ private: DWORD status_exit (DWORD); }; +#define MAX_PID 65536 + #define ISSTATE(p, f) (!!((p)->process_state & f)) #define NOTSTATE(p, f) (!((p)->process_state & f))