aboutsummaryrefslogtreecommitdiff
path: root/src/change_log.cc
blob: 8ff147527c9f2eb7c2300f49974235d653528c70 (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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

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

#include "io.h"
#include "utility.h"
#include "actual_function.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;

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) {
	__attribute__((__noreturn__)) void(*real_exit)(int) = nullptr;
	const void* symbol_address = dlsym(RTLD_NEXT, "exit");

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

	logger->append("exit");

	real_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) {
	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 actual::write(fd, buffer, count);
}

int rename(const char* old_path, const char* new_path) {
	if ( !is_tracked_file(old_path) ) {
		track_file(old_path);
	}

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

	return actual::rename(old_path, new_path);
}

int rmdir(const char* path) {
	logger->append("removed directory '" + std::string(path) + "'");

	return actual::rmdir(path);
}

int unlink(const char* path) {
	logger->append("removed '" + std::string(path) + "'");

	return actual::unlink(path);
}

int unlinkat(int dirfd, const char* path, int flags) {
	if ( dirfd == AT_FDCWD ) {
		logger->append("removed '" + std::string(path) + "'");
	} else {
		logger->append("removed '" + io::get_file_name(dirfd) + path + "'");
	}

	return actual::unlinkat(dirfd, path, flags);
}

void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
	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 actual::mmap(addr, length, prot, flags, fd, offset);
}