aboutsummaryrefslogtreecommitdiff
path: root/src/change_log.cc
blob: f07296e78c7e6e169b3cc3e046fd2c395ee5031e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <sys/mman.h>

#include <memory>
#include <algorithm>
#include <functional>
#include <unordered_set>

#include "io.h"
#include "utility.h"

static std::unique_ptr<std::unordered_set<std::string>> tracked_files;
static std::unique_ptr<io::FileDescriptorGuard>         fd_guard;
static std::unique_ptr<utility::Logger>                 logger;

template <class Result, typename... Arguments>
std::function<Result(Arguments...)> get_real_function(
	const std::string& symbol_name) {

	Result (*real_function)(Arguments...) = nullptr;
	const void* symbol_address = dlsym(RTLD_NEXT, symbol_name.c_str());

	std::memcpy(&real_function, &symbol_address, sizeof(symbol_address));

	return std::function<Result(Arguments...)>(real_function);
}

void init() __attribute__ ((constructor));
void init() {
	tracked_files = std::make_unique<std::unordered_set<std::string>>();

	if ( getenv("CHANGE_LOG_TARGET") != NULL ) {
		fd_guard = std::make_unique<io::FileDescriptorGuard>(
			getenv("CHANGE_LOG_TARGET")
		);
		logger   = std::make_unique<utility::Logger>(*fd_guard);
	} else {
		logger   = std::make_unique<utility::Logger>(STDERR_FILENO);
	}
}

void exit(int status) {
	logger->append("exit");

	get_real_function<void, int>("exit")(status);
}

bool is_tracked_file(const std::string& file_name) {
	return tracked_files->find(file_name) != tracked_files->end();
}

bool track_file(const std::string& file_name) {
	return tracked_files->emplace(file_name).second;
}

ssize_t write(int fd, const void* buffer, size_t count) {
	static auto real_write = get_real_function<ssize_t, int, const void*, size_t>("write");

	if ( io::is_regular_file(fd) ) {
		const std::string file_name{ io::get_file_name(fd) };

		if ( !is_tracked_file(file_name) ) {
			track_file(file_name);

			logger->append("wrote to '" + file_name + "'");
		}
	}

	return real_write(fd, buffer, count);
}

int rename(const char* old_path, const char* new_path) {
	static auto real_rename = get_real_function<int, const char*, const char*>("rename");

	if ( !is_tracked_file(old_path) ) {
		track_file(old_path);
	}

	logger->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'");

	return real_rename(old_path, new_path);
}

int rmdir(const char* path) {
	static auto real_rmdir = get_real_function<int, const char*>("rmdir");

	logger->append("removed directory '" + std::string(path) + "'");

	return real_rmdir(path);
}

int unlink(const char* path) {
	static auto real_unlink = get_real_function<int, const char*>("unlink");

	logger->append("removed '" + std::string(path) + "'");

	return real_unlink(path);
}

int unlinkat(int dirfd, const char* path, int flags) {
	static auto real_unlinkat = get_real_function<int, int, const char*, int>("unlinkat");

	if ( dirfd == AT_FDCWD ) {
		logger->append("removed '" + std::string(path) + "'");
	} else {
		logger->append("removed '" + io::get_file_name(dirfd) + path + "'");
	}

	return real_unlinkat(dirfd, path, flags);
}

void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
	static auto real_mmap = get_real_function<void*, void*, size_t, int, int, int, off_t>("mmap");

	if ( ( prot & PROT_WRITE ) && io::is_regular_file(fd) ) {
		const std::string file_name{ io::get_file_name(fd) };

		if ( !is_tracked_file(file_name) ) {
			track_file(file_name);

			logger->append("mmap '" + file_name + "'");
		}
	}

	return real_mmap(addr, length, prot, flags, fd, offset);
}