summaryrefslogtreecommitdiffstats
path: root/lib/mmapped.c
blob: fa079ad26ded08a5df35df64c13563407511b57c (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
/*  Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
 *  SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>

#include "lib/mmapped.h"
#include "lib/utils.h"

static inline bool fcntl_flock_whole(int fd, short int type, bool wait)
{
	struct flock fl = {
		.l_type   = type,      // F_WRLCK, F_RDLCK, F_UNLCK
		.l_whence = SEEK_SET,
		.l_start  = 0,
		.l_len    = 0 };
	return fcntl(fd, (wait ? F_SETLKW : F_SETLK), &fl) != -1;
}

int mmapped_init(struct mmapped *mmapped, const char *mmap_file, size_t size, void *header, size_t header_size)
{
	int ret = 0;
	int fd = mmapped->fd = open(mmap_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	if (fd == -1) {
		ret = kr_error(errno);
		kr_log_crit(SYSTEM, "Cannot open file %s with shared data: %s\n",
				mmap_file, strerror(errno));
		goto fail;
	}

	// try to acquire write lock; copy header on success
	if (fcntl_flock_whole(fd, F_WRLCK, false)) {
		if (ftruncate(fd, 0) == -1 || ftruncate(fd, size) == -1) {  // get all zeroed
			ret = kr_error(errno);
			kr_log_crit(SYSTEM, "Cannot change size of file %s containing shared data: %s\n",
					mmap_file, strerror(errno));
			goto fail;
		}
		mmapped->mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
		if (mmapped->mem == MAP_FAILED) goto fail_errno;

		memcpy(mmapped->mem, header, header_size);

		return MMAPPED_WAS_FIRST;
	}

	// wait for acquiring shared lock; check header on success
	if (!fcntl_flock_whole(fd, F_RDLCK, true)) goto fail_errno;

	struct stat s;
	bool succ = (fstat(fd, &s) == 0);
	if (!succ) goto fail_errno;
	if (s.st_size != size) goto fail_header_mismatch;

	mmapped->mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (mmapped->mem == MAP_FAILED) goto fail_errno;
	if (memcmp(mmapped->mem, header, header_size) != 0) {
		munmap(mmapped->mem, size);
		goto fail_header_mismatch;
	}

	return 0;


fail_header_mismatch:
	kr_log_crit(SYSTEM, "Another instance of kresd uses file %s with different configuration.\n", mmap_file);
	errno = ENOTRECOVERABLE;

fail_errno:
	ret = kr_error(errno);

fail:
	if (fd >= 0) {
		fcntl_flock_whole(fd, F_UNLCK, false);
		close(fd);
	}
	mmapped->mem = NULL;
	return ret;
}

int mmapped_init_continue(struct mmapped *mmapped)
{
	if (!fcntl_flock_whole(mmapped->fd, F_RDLCK, false)) return kr_error(errno);
	return 0;
}

void mmapped_deinit(struct mmapped *mmapped)
{
	if (mmapped->mem == NULL) return;
	int fd = mmapped->fd;

	munmap(mmapped->mem, mmapped->size);
	mmapped->mem = NULL;

	fcntl_flock_whole(fd, F_UNLCK, false);

	// remove file data unless it is still locked by other processes
	if (fcntl_flock_whole(fd, F_WRLCK, false)) {

		/* If the configuration is updated at runtime, manager may remove the file
		 * and the new processes create it again while old processes are still using the old data.
		 * Here we keep zero-size file not to accidentally remove the new file instead of the old one.
		 * Still truncating the file will cause currently starting processes waiting for read lock on the same file to fail,
		 * but such processes are not expected to exist. */
		ftruncate(fd, 0);

		fcntl_flock_whole(fd, F_UNLCK, false);
	}
	close(fd);
}