Due to deficient testing, the current code doesn't return a valid result
to users of sched_getaffinity().  Carry the result procmask through to
the generation of result cpu mask.

Recognize Windows' limitation that if the process is multi-group (i.e.,
has threads in multiple cpu groups) there is no visibility to which
processors in other groups are being used.  One could remedy this by
looping through all the process' threads, but that could be expensive
so is left for future contemplation.

Reported-by: Christian Franke <christian.fra...@t-online.de>
Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257616.html
Signed-off-by: Mark Geisert <m...@maxrnd.com>
Fixes: 641ecb07533e ("Cygwin: Implement sched_[gs]etaffinity()")

---
 winsup/cygwin/sched.cc | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/sched.cc b/winsup/cygwin/sched.cc
index 2f4fbc31a..ce1e94345 100644
--- a/winsup/cygwin/sched.cc
+++ b/winsup/cygwin/sched.cc
@@ -588,8 +588,11 @@ __sched_getaffinity_sys (pid_t pid, size_t sizeof_set, 
cpu_set_t *set)
        }
 
       KAFFINITY miscmask = groupmask (__get_cpus_per_group ());
+      /* if process is multi-group, we don't have processor visibility. */
+      if (procmask == 0)
+       procmask = miscmask;
       for (int i = 0; i < groupcount; i++)
-       setgroup (sizeof_set, set, grouparray[i], miscmask);
+       setgroup (sizeof_set, set, grouparray[i], miscmask & procmask);
     }
   else
     status = ESRCH;
-- 
2.45.1

Reply via email to