Re: libcruft/sysconf_cpus.c patch

Felix von Leitner <[email protected]> Tue, 23 Nov 2010 04:41:39 +0100
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
> __sc_nr_cpus() [ == sysconf(_SC_NPROCESSORS_ONLN) ] can run off the end
> of its /proc/cpuid buffer and (depending on process memory layout) dump
> core.  This caused an intermittent dump in diet `x86info`.

Thanks, added patch.

> This will still get a wrong answer if "processor" or one of the
> other-arch strings happens to get cut by the buffer end.  Not important
> for my application.  I would suggest a malloc'd buffer the full stat'd
> size of /proc/cpuid, but that seems a bit scary with 160-core x86 boxes
> running around out there this year...

The problem is that stat always returns size 0.
So you'd have to have a loop and try bigger and bigger sizes.  That's
too much effort to get the number of CPUs.  I'm sure there must be some
smarter way to do this than parsing /proc/cpuinfo.

Maybe we should use /proc/stat.  There is no sysctl for this,
unfortunately, and it's not in the ELF auxvec.  Too bad, really.
I'd prefer a way that works if /proc if not mounted.

Felix