gcc -shared -fPIC fake-cpu-count.c -o fake-cpu-count.so -ldl
javac ReportCpuCount.java
java -cp . ReportCpuCount
#Found 8 CPUs
#...
LD_PRELOAD=./fake-cpu-count.so java -cp . ReportCpuCount
#Found 2 CPUs
#...
#define _GNU_SOURCE | |
#include "stdlib.h" | |
#include <unistd.h> | |
#include <dlfcn.h> | |
typedef long int (*orig_sysconf_f_type)(int __name); | |
long sysconf(int name){ | |
orig_sysconf_f_type orig_sysconf; | |
orig_sysconf = (orig_sysconf_f_type)dlsym(RTLD_NEXT,"sysconf"); | |
if (name == _SC_NPROCESSORS_CONF){ | |
return 2; | |
} | |
return orig_sysconf(name); | |
} |
public class ReportCpuCount { | |
public static void main(String[] args) throws Exception{ | |
while(true){ | |
System.out.printf("#Found %d CPUs%n", Runtime.getRuntime().availableProcessors()); | |
Thread.sleep(1000); | |
} | |
} | |
} |