aboutsummaryrefslogtreecommitdiff
path: root/src/actual.h
blob: af64ec3c434fd61c98ab589d6637fa64271ad494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifndef CHANGE_SRC_ACTUAL_FUNCTION_H_
#define CHANGE_SRC_ACTUAL_FUNCTION_H_

#include <dlfcn.h>
#include <sys/mman.h>
#include <sys/uio.h>

#include <memory>
#include <cstring>

namespace {
	template <class Result, typename... Arguments>
	using function_ptr = Result(*)(Arguments...);

	template <typename FunctionPtr>
	FunctionPtr get_actual_function(const std::string& symbol_name) {
		const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) };

		FunctionPtr actual_function{};
		std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address));

		return actual_function;
	}
}

namespace actual {
	static auto write = get_actual_function<
		function_ptr<ssize_t, int, const void*, size_t>
	>("write");

	static auto writev = get_actual_function<
		function_ptr<ssize_t, int, const iovec*, int>
	>("writev");

	static auto rename = get_actual_function<
		function_ptr<int, const char*, const char*>
	>("rename");

	static auto rmdir = get_actual_function<
		function_ptr<int, const char*>
	>("rmdir");

	static auto unlink = get_actual_function<
		function_ptr<int, const char*>
	>("unlink");

	static auto unlinkat = get_actual_function<
		function_ptr<int, int, const char*, int>
	>("unlinkat");

	static auto mmap = get_actual_function<
		function_ptr<void*, void*, size_t, int, int, int, off_t>
	>("mmap");
}

#endif  // CHANGE_SRC_ACTUAL_FUNCTION_H_