LINUX.ORG.RU

История изменений

Исправление Deleted, (текущая версия) :

#define _GNU_SOURCE

#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <dlfcn.h>

typedef struct {
	const char *orig_path;
	const char *new_path;
} Replacement;

static const Replacement replacements[] = {
	{
		.orig_path = "/proc/cpuinfo",
		.new_path = "/tmp/cpuinfo.replacement",
	},

	{
		.orig_path = "/sys/devices/system/cpu/online",
		.new_path = "/tmp/cpu-online.replacement",
	},

	{
		.orig_path = NULL,
	       	.new_path = NULL,
	},
};

typedef int (* OpenFn)(const char *pathname, int flags, ...);

int open(const char *pathname, int flags, ...)
{
	OpenFn real_open_fn = dlsym(RTLD_NEXT, "open");

	mode_t mode = 0;
	bool have_mode = false;
	va_list ap;
	va_start(ap, flags);
	if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
		mode = va_arg(ap, mode_t);
		have_mode = true;
	}
	va_end(ap);

	const char *new_path = pathname;
	char *orig_path = realpath(pathname, NULL);
	if (orig_path) {
		for (const Replacement *replacement = replacements; replacement->orig_path; ++replacement) {
			if (!strcmp(orig_path, replacement->orig_path)) {
				new_path = replacement->new_path;
				break;
			}
		}
		free(orig_path);
	}

	if (have_mode) {
		return real_open_fn(new_path, flags, mode);
	} else {
		return real_open_fn(new_path, flags);
	}
}

Собрать:

gcc -std=c11 -Wall -Wextra -fPIC -shared -ldl -o replace-open-file.so replace-open-file.c
Тест:
$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 78
model name      : Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
stepping        : 3
...

$ cat /sys/devices/system/cpu/online
0-3

$ LD_PRELOAD=./replace-open-file.so cat /proc/cpuinfo
Процессоры не найдены

$ LD_PRELOAD=./replace-open-file.so cat /sys/devices/system/cpu/online
Ты наркоман? Нет тут процессоров!

Если для подсчёта ядер используется что-то другое, то можно выяснить и так же подменить. Если номера ядер как-то используются, то всё сложнее, но тоже теоретически можно подхачить.

Исходная версия Deleted, :

Вот пример...

#define _GNU_SOURCE

#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <dlfcn.h>

typedef struct {
	const char *orig_path;
	const char *new_path;
} Replacement;

static const Replacement replacements[] = {
	{
		.orig_path = "/proc/cpuinfo",
		.new_path = "/tmp/cpuinfo.replacement",
	},

	{
		.orig_path = "/sys/devices/system/cpu/online",
		.new_path = "/tmp/cpu-online.replacement",
	},

	{
		.orig_path = NULL,
	       	.new_path = NULL,
	},
};

typedef int (* OpenFn)(const char *pathname, int flags, ...);

int open(const char *pathname, int flags, ...)
{
	OpenFn real_open_fn = dlsym(RTLD_NEXT, "open");

	mode_t mode = 0;
	bool have_mode = false;
	va_list ap;
	va_start(ap, flags);
	if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
		mode = va_arg(ap, mode_t);
		have_mode = true;
	}
	va_end(ap);

	const char *new_path = pathname;
	char *orig_path = realpath(pathname, NULL);
	if (orig_path) {
		for (const Replacement *replacement = replacements; replacement->orig_path; ++replacement) {
			if (!strcmp(orig_path, replacement->orig_path)) {
				new_path = replacement->new_path;
				break;
			}
		}
		free(orig_path);
	}

	if (have_mode) {
		return real_open_fn(new_path, flags, mode);
	} else {
		return real_open_fn(new_path, flags);
	}
}

Собрать:

gcc -std=c11 -Wall -Wextra -fPIC -shared -ldl -o replace-open-file.so replace-open-file.c
Тест:
$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 78
model name      : Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
stepping        : 3
...

$ cat /sys/devices/system/cpu/online
0-3

$ LD_PRELOAD=./replace-open-file.so cat /proc/cpuinfo
Процессоры не найдены

$ LD_PRELOAD=./replace-open-file.so cat /sys/devices/system/cpu/online
Ты наркоман? Нет тут процессоров!