aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/gen/sched_getaffinity.c
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 /lib/libc/gen/sched_getaffinity.c
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
Diffstat (limited to 'lib/libc/gen/sched_getaffinity.c')
-rw-r--r--lib/libc/gen/sched_getaffinity.c20
1 files changed, 20 insertions, 0 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));
}