git: 8c709f30a184 - stable/13 - sched_get/setaffinity(): try to be more compatible with Linux
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 14 Jan 2022 18:10:37 UTC
The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=8c709f30a18466527e52e3b081a69fd2b99b6a19 commit 8c709f30a18466527e52e3b081a69fd2b99b6a19 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2022-01-02 22:11:49 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2022-01-14 16:17:31 +0000 sched_get/setaffinity(): try to be more compatible with Linux (cherry picked from commit d9cacbf4b010d96dbfe96a09259972a95c675f26) --- lib/libc/gen/sched_getaffinity.c | 20 ++++++++++++++++++++ lib/libc/gen/sched_setaffinity.c | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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)); }