From: Willy Tarreau Date: Thu, 13 Mar 2025 12:35:30 +0000 (+0100) Subject: DEV: ncpu: also emulate sysconf() for _SC_NPROCESSORS_* X-Git-Tag: v3.2-dev8~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ceb1f2c515ca437a7ccc9841f647bcd2ac89757;p=thirdparty%2Fhaproxy.git DEV: ncpu: also emulate sysconf() for _SC_NPROCESSORS_* This is also needed in order to make the requested number of CPUs appear. For now we don't reroute to the original sysconf() call so we return -1,EINVAL for all other info. --- diff --git a/dev/ncpu/ncpu.c b/dev/ncpu/ncpu.c index cdc91eb33e..e96961c095 100644 --- a/dev/ncpu/ncpu.c +++ b/dev/ncpu/ncpu.c @@ -1,4 +1,5 @@ #define _GNU_SOURCE +#include #include #include #include @@ -11,6 +12,22 @@ static char prog_full_path[PATH_MAX]; +long sysconf(int name) +{ + if (name == _SC_NPROCESSORS_ONLN || + name == _SC_NPROCESSORS_CONF) { + const char *ncpu = getenv("NCPU"); + int n; + + n = ncpu ? atoi(ncpu) : CPU_SETSIZE; + if (n < 0 || n > CPU_SETSIZE) + n = CPU_SETSIZE; + return n; + } + errno = EINVAL; + return -1; +} + /* return a cpu_set having the first $NCPU set */ int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) {