summaryrefslogtreecommitdiffstats
path: root/src/lib-master/master-instance.c
blob: 86fb1c142468de1e3e4845ef6eb2dba2de370f0a (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "path-util.h"
#include "array.h"
#include "istream.h"
#include "ostream.h"
#include "file-dotlock.h"
#include "str.h"
#include "strescape.h"
#include "master-instance.h"

#include <unistd.h>
#include <fcntl.h>

struct master_instance_list {
	pool_t pool;
	const char *path;

	ARRAY(struct master_instance) instances;

	bool locked:1;
	bool config_paths_changed:1;
};

struct master_instance_list_iter {
	struct master_instance_list *list;
	unsigned int idx;
};

static const struct dotlock_settings dotlock_set = {
	.timeout = 10,
	.stale_timeout = 60,
	.use_io_notify = TRUE,
};

struct master_instance_list *master_instance_list_init(const char *path)
{
	struct master_instance_list *list;
	pool_t pool;

	pool = pool_alloconly_create(MEMPOOL_GROWING"master instances", 256);
	list = p_new(pool, struct master_instance_list, 1);
	list->pool = pool;
	list->path = p_strdup(pool, path);
	p_array_init(&list->instances, pool, 8);
	return list;
}

void master_instance_list_deinit(struct master_instance_list **_list)
{
	struct master_instance_list *list = *_list;

	*_list = NULL;
	pool_unref(&list->pool);
}

static void
master_instance_update_config_path(struct master_instance_list *list,
				   struct master_instance *inst)
{
	const char *path, *config_path, *error;

	/* update instance's config path if it has changed */
	path = t_strconcat(inst->base_dir, "/"PACKAGE".conf", NULL);
	if (t_readlink(path, &config_path, &error) < 0) {
		/* The link may not exist, ignore the error. */
		if (errno != ENOENT)
			i_error("t_readlink(%s) failed: %s", path, error);
		return;
	}
	if (null_strcmp(inst->config_path, config_path) != 0) {
		inst->config_path = p_strdup(list->pool, config_path);
		list->config_paths_changed = TRUE;
	}
}

static int
master_instance_list_add_line(struct master_instance_list *list,
			      const char *line)
{
	struct master_instance *inst;
	const char *const *args;
	time_t last_used;

	/* <last used> <name> <base dir> [<config path>] */
	args = t_strsplit_tabescaped(line);
	if (str_array_length(args) < 3)
		return -1;
	if (str_to_time(args[0], &last_used) < 0)
		return -1;

	inst = array_append_space(&list->instances);
	inst->last_used = last_used;
	inst->name = p_strdup(list->pool, args[1]);
	inst->base_dir = p_strdup(list->pool, args[2]);
	inst->config_path = p_strdup_empty(list->pool, args[3]);
	master_instance_update_config_path(list, inst);
	return 0;
}

static int master_instance_list_refresh(struct master_instance_list *list)
{
	struct istream *input;
	const char *line;
	int fd, ret = 0;

	array_clear(&list->instances);

	fd = open(list->path, O_RDONLY);
	if (fd == -1) {
		if (errno == ENOENT)
			return 0;

		i_error("open(%s) failed: %m", list->path);
		return -1;
	}
	input = i_stream_create_fd_autoclose(&fd, SIZE_MAX);
	while ((line = i_stream_read_next_line(input)) != NULL) T_BEGIN {
		if (master_instance_list_add_line(list, line) < 0)
			i_error("Invalid line in %s: %s", list->path, line);
	} T_END;
	if (input->stream_errno != 0) {
		i_error("read(%s) failed: %s", list->path,
			i_stream_get_error(input));
		ret = -1;
	}
	i_stream_destroy(&input);
	return ret;
}

static int
master_instance_list_write(struct master_instance_list *list,
			   int fd, const char *path)
{
	struct ostream *output;
	const struct master_instance *inst;
	string_t *str = t_str_new(128);
	int ret = 0;

	output = o_stream_create_fd(fd, 0);
	o_stream_cork(output);
	array_foreach(&list->instances, inst) {
		str_truncate(str, 0);
		str_printfa(str, "%ld\t", (long)inst->last_used);
		str_append_tabescaped(str, inst->name);
		str_append_c(str, '\t');
		str_append_tabescaped(str, inst->base_dir);
		str_append_c(str, '\t');
		if (inst->config_path != NULL)
			str_append_tabescaped(str, inst->config_path);
		str_append_c(str, '\n');
		o_stream_nsend(output, str_data(str), str_len(str));
	}
	if (o_stream_finish(output) < 0) {
		i_error("write(%s) failed: %s", path, o_stream_get_error(output));
		ret = -1;
	}
	o_stream_destroy(&output);
	return ret;
}

static int master_instance_write_init(struct master_instance_list *list,
				      struct dotlock **dotlock_r)
{
	int fd;

	i_assert(!list->locked);

	*dotlock_r = NULL;

	fd = file_dotlock_open_mode(&dotlock_set, list->path, 0, 0644,
				    (uid_t)-1, (gid_t)-1, dotlock_r);
	if (fd == -1) {
		i_error("file_dotlock_open(%s) failed: %m", list->path);
		return -1;
	}
	if (master_instance_list_refresh(list) < 0) {
		file_dotlock_delete(dotlock_r);
		return -1;
	}
	list->locked = TRUE;
	return fd;
}

static int master_instance_write_finish(struct master_instance_list *list,
					int fd, struct dotlock **dotlock)
{
	const char *lock_path = file_dotlock_get_lock_path(*dotlock);
	int ret;

	i_assert(list->locked);

	T_BEGIN {
		ret = master_instance_list_write(list, fd, lock_path);
	} T_END;

	list->locked = FALSE;
	if (ret < 0) {
		file_dotlock_delete(dotlock);
		return -1;
	}
	if (fdatasync(fd) < 0) {
		i_error("fdatasync(%s) failed: %m", lock_path);
		file_dotlock_delete(dotlock);
		return -1;
	}
	list->config_paths_changed = FALSE;
	return file_dotlock_replace(dotlock, 0);
}

static struct master_instance *
master_instance_find(struct master_instance_list *list,
		     const char *base_dir)
{
	struct master_instance *inst;

	array_foreach_modifiable(&list->instances, inst) {
		if (strcmp(inst->base_dir, base_dir) == 0)
			return inst;
	}
	return NULL;
}

int master_instance_list_update(struct master_instance_list *list,
				const char *base_dir)
{
	struct master_instance *inst;
	struct dotlock *dotlock;
	int fd;

	if ((fd = master_instance_write_init(list, &dotlock)) == -1)
		return -1;

	inst = master_instance_find(list, base_dir);
	if (inst == NULL) {
		inst = array_append_space(&list->instances);
		inst->name = "";
		inst->base_dir = p_strdup(list->pool, base_dir);
	}
	inst->last_used = time(NULL);
	master_instance_update_config_path(list, inst);

	return master_instance_write_finish(list, fd, &dotlock);
}

int master_instance_list_set_name(struct master_instance_list *list,
				  const char *base_dir, const char *name)
{
	const struct master_instance *orig_inst;
	struct master_instance *inst;
	struct dotlock *dotlock;
	int fd;

	i_assert(*name != '\0');

	if ((fd = master_instance_write_init(list, &dotlock)) == -1)
		return -1;

	orig_inst = master_instance_list_find_by_name(list, name);
	if (orig_inst != NULL &&
	    strcmp(orig_inst->base_dir, base_dir) != 0) {
		/* name already used */
		file_dotlock_delete(&dotlock);
		list->locked = FALSE;
		return 0;
	}

	inst = master_instance_find(list, base_dir);
	if (inst == NULL) {
		inst = array_append_space(&list->instances);
		inst->base_dir = p_strdup(list->pool, base_dir);
	}
	inst->name = p_strdup(list->pool, name);
	inst->last_used = time(NULL);

	return master_instance_write_finish(list, fd, &dotlock) < 0 ? -1 : 1;
}

int master_instance_list_remove(struct master_instance_list *list,
				const char *base_dir)
{
	struct dotlock *dotlock;
	const struct master_instance *instances;
	unsigned int i, count;
	int fd;

	if ((fd = master_instance_write_init(list, &dotlock)) == -1)
		return -1;

	instances = array_get(&list->instances, &count);
	for (i = 0; i < count; i++) {
		if (strcmp(instances[i].base_dir, base_dir) == 0) {
			array_delete(&list->instances, i, 1);
			break;
		}
	}

	if (i == count) {
		file_dotlock_delete(&dotlock);
		list->locked = FALSE;
		return 0;
	}
	return master_instance_write_finish(list, fd, &dotlock) < 0 ? -1 : 1;
}

static int
master_instance_list_refresh_and_update(struct master_instance_list *list)
{
	struct dotlock *dotlock;
	int fd;

	if (master_instance_list_refresh(list) < 0)
		return -1;
	if (list->config_paths_changed && !list->locked) {
		/* write new config paths */
		if ((fd = master_instance_write_init(list, &dotlock)) == -1)
			return -1;
		if (master_instance_write_finish(list, fd, &dotlock) < 0)
			return -1;
	}
	return 0;
}

const struct master_instance *
master_instance_list_find_by_name(struct master_instance_list *list,
				  const char *name)
{
	const struct master_instance *inst;

	i_assert(*name != '\0');

	if (array_count(&list->instances) == 0)
		(void)master_instance_list_refresh_and_update(list);

	array_foreach(&list->instances, inst) {
		if (strcmp(inst->name, name) == 0)
			return inst;
	}
	return NULL;
}

struct master_instance_list_iter *
master_instance_list_iterate_init(struct master_instance_list *list)
{
	struct master_instance_list_iter *iter;

	iter = i_new(struct master_instance_list_iter, 1);
	iter->list = list;
	(void)master_instance_list_refresh_and_update(list);
	return iter;
}

const struct master_instance *
master_instance_iterate_list_next(struct master_instance_list_iter *iter)
{
	if (iter->idx == array_count(&iter->list->instances))
		return NULL;
	return array_idx(&iter->list->instances, iter->idx++);
}

void master_instance_iterate_list_deinit(struct master_instance_list_iter **_iter)
{
	struct master_instance_list_iter *iter = *_iter;

	*_iter = NULL;

	i_free(iter);
}