aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2022-01-02 22:11:49 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2022-01-03 02:31:40 +0000
commitd9cacbf4b010d96dbfe96a09259972a95c675f26 (patch)
tree24e8b18bbf973f80ec8089c5f95b22655cfbadbe
parent90266521018938b7b9f0003ba9a383b6920859e9 (diff)
downloadsrc-d9cacbf4b010d96dbfe96a09259972a95c675f26.tar.gz
src-d9cacbf4b010d96dbfe96a09259972a95c675f26.zip
sched_get/setaffinity(): try to be more compatible with Linux
in handling the cpuset sizes different from sizeof(cpuset_t). For both cases, cpuset size shorter than sizeof(cpuset_t) results in EINVAL on Linux. For sched_getaffinity(), be more permissive and accept cpuset size larger than our cpuset_t, by clipping the syscall argument and zeroing the rest of the output buffer. For sched_setaffinity(), we should allow shorter cpusets than current ABI size, again zeroing the rest of the bits. With this change, python os.sched_get/setaffinity functions work. Reported by: se Sponsored by: The FreeBSD Foundation MFC after: 1 week
-rw-r--r--lib/libc/gen/sched_getaffinity.c20
-rw-r--r--lib/libc/gen/sched_setaffinity.c13
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c
index 2ae8c5b763a3..7d345eb82a3b 100644
--- a/lib/libc/gen/sched_getaffinity.c
+++ b/lib/libc/gen/sched_getaffinity.c
@@ -26,11 +26,31 @@
* SUCH DAMAGE.
*/
+#include <errno.h>
#include <sched.h>
+#include <string.h>
int
sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset)
{
+ /*
+ * Be more Linux-compatible:
+ * - return EINVAL in passed size is less than size of cpuset_t
+ * in advance, instead of ERANGE from the syscall
+ * - if passed size is larger than the size of cpuset_t, be
+ * permissive by claming it back to sizeof(cpuset_t) and
+ * zeroing the rest.
+ */
+ if (cpusetsz < sizeof(cpuset_t)) {
+ errno = EINVAL;
+ return (-1);
+ }
+ if (cpusetsz > sizeof(cpuset_t)) {
+ memset((char *)cpuset + sizeof(cpuset_t), 0,
+ cpusetsz - sizeof(cpuset_t));
+ cpusetsz = sizeof(cpuset_t);
+ }
+
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 52fc6d4bc811..91ab2a401bda 100644
--- a/lib/libc/gen/sched_setaffinity.c
+++ b/lib/libc/gen/sched_setaffinity.c
@@ -26,11 +26,22 @@
* SUCH DAMAGE.
*/
+#include <errno.h>
#include <sched.h>
+#include <string.h>
int
sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset)
{
+ cpuset_t c;
+
+ if (cpusetsz > sizeof(cpuset_t)) {
+ errno = EINVAL;
+ return (-1);
+ } else {
+ memset(&c, 0, sizeof(c));
+ memcpy(&c, cpuset, cpusetsz);
+ }
return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
- pid == 0 ? -1 : pid, cpusetsz, cpuset));
+ pid == 0 ? -1 : pid, sizeof(cpuset_t), &c));
}