From e2b17f5e0cd37b2ca891401ab6074f454d093dab Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 28 Jul 2022 08:00:40 -0500 Subject: [PATCH] sysconf(NPROCESSORS_CONF) is broken on musl and they've been arguing about it for years (https://www.openwall.com/lists/musl/2022/07/27/5) so just inline the readdir glibc and bionic are both doing under the covers. In theory we could also readdir for the online case, but the syscall works when sysfs isn't mounted. (And while I'm there: never return 0.) --- toys/other/taskset.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/toys/other/taskset.c b/toys/other/taskset.c index 6da9ec7da..aae8ce212 100644 --- a/toys/other/taskset.c +++ b/toys/other/taskset.c @@ -114,15 +114,26 @@ void taskset_main(void) void nproc_main(void) { unsigned i, j, nproc = 0; + DIR *dd; // This can only detect 32768 processors. Call getaffinity and count bits. - if (!toys.optflags && -1!=sched_getaffinity(getpid(), 4096, toybuf)) { + if (!toys.optflags && -1!=sched_getaffinity(getpid(), 4096, toybuf)) for (i = 0; i<4096; i++) if (toybuf[i]) for (j=0; j<8; j++) if (toybuf[i]&(1<d_name, "cpu", 3)) continue; + for (ss = de->d_name+3; isdigit(*ss); ss++); + if (!*ss) nproc++; + } + closedir(dd); + } - xprintf("%u\n", nproc); + xprintf("%u\n", nproc ? : 1); }