[Bug 260487] sched_getaffinity(3)/sched_setaffinity(3): wrong handling of PID 0

From: <bugzilla-noreply_at_freebsd.org>
Date: Fri, 17 Dec 2021 05:21:04 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=260487

            Bug ID: 260487
           Summary: sched_getaffinity(3)/sched_setaffinity(3): wrong
                    handling of PID 0
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: sigsys@gmail.com

sched_getaffinity(3)/sched_setaffinity(3) are documented to use PID 0 to refer
to the current process, but the underlying
cpuset_getaffinity(2)/cpuset_setaffinity(2) only do so with PID -1.

I noticed FFmpeg library code passing PID 0 to sched_getaffinity().

diff --git a/lib/libc/gen/sched_getaffinity.c
b/lib/libc/gen/sched_getaffinity.c
index 5557d3d93b47..7fca53e39b4b 100644
--- a/lib/libc/gen/sched_getaffinity.c
+++ b/lib/libc/gen/sched_getaffinity.c
@@ -32,6 +32,6 @@
 int
 sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset)
 {
-       return (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
-           cpusetsz, cpuset));
+       return (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
+           pid == 0 ? -1 : pid, cpusetsz, cpuset));
 }
diff --git a/lib/libc/gen/sched_setaffinity.c
b/lib/libc/gen/sched_setaffinity.c
index ad775b5dbce5..1c083b4b108a 100644
--- a/lib/libc/gen/sched_setaffinity.c
+++ b/lib/libc/gen/sched_setaffinity.c
@@ -32,6 +32,6 @@
 int
 sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset)
 {
-       return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
-           cpusetsz, cpuset));
+       return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
+           pid == 0 ? -1 : pid, cpusetsz, cpuset));
 }

-- 
You are receiving this mail because:
You are the assignee for the bug.