As far as I can tell, syscall() is supposed to set errno (using __sycall_ret), but maybe src/misc/syscall.c isn't what I think it is?
I indeed got the return value backwards, and I'll fix that, along with the #ifdef.
Actually, nope, that ternary is the right way round, and __sycall_ret handles the -1 return.
On Mon, 29 Feb 2016, Nathan Zadoks wrote:
> This is a GNU extension, but a fairly minor one, for a system call that
> otherwise has no libc wrapper.
>
> Adding it was discussed previously, without any objections:
> http://www.openwall.com/lists/musl/2015/05/08/24
> ---
> include/sched.h | 3 +++
> src/sched/sched_getcpu.c | 10 ++++++++++
> 2 files changed, 13 insertions(+)
> create mode 100644 src/sched/sched_getcpu.c
>
> diff --git a/include/sched.h b/include/sched.h
> index 3e34a72..17f5e06 100644
> --- a/include/sched.h
> +++ b/include/sched.h
> @@ -76,6 +76,9 @@ void free(void *);
>
> typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t;
> int __sched_cpucount(size_t, const cpu_set_t *);
> +#ifdef _GNU_SOURCE
This code is already under the same #ifdef.
> +int sched_getcpu(void);
> +#endif
> int sched_getaffinity(pid_t, size_t, cpu_set_t *);
> int sched_setaffinity(pid_t, size_t, const cpu_set_t *);
>
> diff --git a/src/sched/sched_getcpu.c b/src/sched/sched_getcpu.c
> new file mode 100644
> index 0000000..070d6e7
> --- /dev/null
> +++ b/src/sched/sched_getcpu.c
> @@ -0,0 +1,10 @@
> +#define _GNU_SOURCE
> +#include <stdlib.h>
Do you need this include?
> +#include <sched.h>
(this include could also be dropped; I think it's a matter of policy whether
such includes are desirable or not, so please wait for comment from Rich)
> +#include "syscall.h"
> +
> +int sched_getcpu(void) {
> + int c, s;
> + s = syscall(SYS_getcpu, &c, NULL, NULL);
> + return (s == 0) ? c : s;
This is wrong, as it doesn't set errno on error, and does not produce -1. This
should be something like 'return s ? __syscall_ret(s) : c;' or maybe
'return __syscall_ret(s ? s : c);'.
Alexander