docker-numcpu_override/numcpu_override.c
nyyu 853378b46d
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: init
2024-04-30 09:31:02 +02:00

33 lines
721 B
C

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
typedef long (*orig_sysconf_f_type)(int name);
long sysconf(int name) {
orig_sysconf_f_type orig;
orig = (orig_sysconf_f_type)dlsym(RTLD_NEXT, "sysconf");
if (name != _SC_NPROCESSORS_CONF && name != _SC_NPROCESSORS_ONLN) {
return orig(name);
}
printf("Overriding call sysconf %d\n", name);
long numcpus = 0;
char *env = getenv("NUMCPUS");
printf("Env NUMCPUS %s\n", env);
if (env != NULL && strlen(env) > 0) {
numcpus = strtol(env, NULL, 10);
}
if (numcpus == 0) {
numcpus = orig(name);
}
printf("Return %ld\n", numcpus);
fflush(stdout);
return numcpus;
}