summaryrefslogtreecommitdiff
path: root/ctdb/failover/statd_callout.c
blob: 4111d67510093dc2b6a663cd9226e76ab19f9cff (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
   CTDB NFSv3 rpc.statd HA callout

   Copyright 2023, DataDirect Networks, Inc. All rights reserved.
   Author: Martin Schwenke <mschwenke@ddn.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "replace.h"

/*
 * A configuration file, created by statd_callout_helper, containing
 * at least 1 line of text.
 *
 * The first line is the mode.  Currently supported modes are:
 *
 *   persistent_db
 *   shared_dir
 *   none
 *
 * In persistent_db and shared_dir modes, the file contains 2
 * subsequent lines of text:
 *
 *   path: directory where files should be created
 *   ips_file: file containing node's currently assigned public IP addresses
 *
 * In none mode, there are no subsequent lines.
 */
#define CONFIG_FILE CTDB_VARDIR "/scripts/statd_callout.conf"

static const char *progname;

struct {
	enum {
		CTDB_SC_MODE_PERSISTENT_DB,
		CTDB_SC_MODE_SHARED_DIR,
		CTDB_SC_MODE_NONE,
	} mode;
	union {
		struct {
			char *path;
			char *ips_file;
		};
	};
} config;

static bool getline_strip(char **lineptr,
			  size_t *n,
			  FILE *stream)
{
	bool was_null;
	int ret;

	was_null = *lineptr == NULL;

	ret = getline(lineptr, n, stream);
	if (ret == -1 || ret <= 2) {
		if (was_null) {
			free(*lineptr);
			*lineptr = NULL;
			*n = 0;
		}
		return false;
	}

	if ((*lineptr)[ret - 1] == '\n') {
		(*lineptr)[ret - 1] = '\0';
	}

	return true;
}

static void free_config(void)
{
	switch (config.mode) {
	case CTDB_SC_MODE_PERSISTENT_DB:
	case CTDB_SC_MODE_SHARED_DIR:
		free(config.path);
		config.path = NULL;
		free(config.ips_file);
		config.ips_file = NULL;
		break;
	case CTDB_SC_MODE_NONE:
		break;
	}
}

static void read_config(void)
{
	const char *config_file = NULL;
	FILE *f = NULL;
	char *mode = NULL;
	size_t n = 0;
	bool status;

	/* For testing only */
	config_file = getenv("CTDB_STATD_CALLOUT_CONFIG_FILE");
	if (config_file == NULL || strlen(config_file) == 0) {
		config_file = CONFIG_FILE;
	}

	f = fopen(config_file, "r");
	if (f == NULL) {
		fprintf(stderr,
			"%s: unable to open config file (%s)\n",
			progname,
			config_file);
		exit(1);
	}

	status = getline_strip(&mode, &n, f);
	if (!status) {
		fprintf(stderr,
			"%s: error parsing mode in %s\n",
			progname,
			config_file);
		exit(1);
	}
	if (strcmp(mode, "persistent_db") == 0) {
		config.mode = CTDB_SC_MODE_PERSISTENT_DB;
	} else if (strcmp(mode, "shared_dir") == 0) {
		config.mode = CTDB_SC_MODE_SHARED_DIR;
	} else if (strcmp(mode, "none") == 0) {
		config.mode = CTDB_SC_MODE_NONE;
	} else {
		fprintf(stderr,
			"%s: unknown mode=%s in %s\n",
			progname,
			mode,
			config_file);
		free(mode);
		exit(1);
	}
	free(mode);

	switch (config.mode) {
	case CTDB_SC_MODE_PERSISTENT_DB:
	case CTDB_SC_MODE_SHARED_DIR:
		status = getline_strip(&config.path, &n, f);
		if (!status) {
			goto parse_error;
		}

		status = getline_strip(&config.ips_file, &n, f);
		if (!status) {
			goto parse_error;
		}

		break;
	case CTDB_SC_MODE_NONE:
		break;
	}

	fclose(f);
	return;

parse_error:
	fprintf(stderr,
		"%s: error parsing contents of %s\n",
		progname,
		config_file);
	free_config();
	exit(1);
}

static void for_each_sip(void (*line_func)(const char *sip, const char *cip),
			 const char *cip)
{
	FILE *f = NULL;
	char *line = NULL;
	size_t n = 0;

	f = fopen(config.ips_file, "r");
	if (f == NULL) {
		fprintf(stderr,
			"%s: unable to open IPs file (%s)\n",
			progname,
			config.ips_file);
		exit(1);
	}

	for (;;) {
		bool status;

		status = getline_strip(&line, &n, f);
		if (!status) {
			if (!feof(f)) {
				fprintf(stderr,
					"%s: error parsing contents of %s\n",
					progname,
					config.ips_file);
				free(line);
				exit(1);
			}
			break;
		}

		line_func(line, cip);
	}

	free(line);
	fclose(f);
	return;
}

static void make_path(char *buf,
		      size_t num,
		      const char *sip,
		      const char *cip)
{
	int ret = snprintf(buf,
			   num,
			   "%s/statd-state@%s@%s",
			   config.path,
			   sip,
			   cip);
	if (ret < 0) {
		/* Not possible for snprintf(3)? */
		fprintf(stderr,
			"%s: error constructing path %s/statd-state@%s@%s\n",
			progname,
			config.path,
			sip,
			cip);
		exit(1);
	}
	if ((size_t)ret >= num) {
		fprintf(stderr,
			"%s: path too long %s/statd-state@%s@%s\n",
			progname,
			config.path,
			sip,
			cip);
		exit(1);
	}
}

static void add_client_persistent_db_line(const char *sip, const char *cip)
{
	char path[PATH_MAX];
	FILE *f;
	long long datetime;

	make_path(path, sizeof(path), sip, cip);

	datetime = (long long)time(NULL);

	f = fopen(path, "w");
	if (f == NULL) {
		fprintf(stderr,
			"%s: unable to open for writing %s\n",
			progname,
			path);
		exit(1);
	}
	fprintf(f, "\"statd-state@%s@%s\" \"%lld\"\n", sip, cip, datetime);
	fclose(f);
}

static void add_client_persistent_db(const char *cip)
{
	for_each_sip(add_client_persistent_db_line, cip);
}

static void del_client_persistent_db_line(const char *sip, const char *cip)
{
	char path[PATH_MAX];
	FILE *f;

	make_path(path, sizeof(path), sip, cip);

	f = fopen(path, "w");
	if (f == NULL) {
		fprintf(stderr,
			"%s: unable to open for writing %s\n",
			progname,
			path);
		exit(1);
	}
	fprintf(f, "\"statd-state@%s@%s\" \"\"\n", sip, cip);
	fclose(f);
}

static void del_client_persistent_db(const char *cip)
{
	for_each_sip(del_client_persistent_db_line, cip);
}

static void add_client_shared_dir_line(const char *sip, const char *cip)
{
	char path[PATH_MAX];
	FILE *f;

	make_path(path, sizeof(path), sip, cip);

	f = fopen(path, "w");
	if (f == NULL) {
		fprintf(stderr,
			"%s: unable to open for writing %s\n",
			progname,
			path);
		exit(1);
	}
	fclose(f);
}

static void add_client_shared_dir(const char *cip)
{
	for_each_sip(add_client_shared_dir_line, cip);
}

static void del_client_shared_dir_line(const char *sip, const char *cip)
{
	char path[PATH_MAX];
	int ret;

	make_path(path, sizeof(path), sip, cip);

	ret = unlink(path);
	if (ret != 0) {
		fprintf(stderr,
			"%s: unable to remove %s\n",
			progname,
			path);
		exit(1);
	}
}

static void del_client_shared_dir(const char *cip)
{
	for_each_sip(del_client_shared_dir_line, cip);
}

static void usage(void)
{
	printf("usage: %s { add-client | del-client } <client-ip>\n", progname);
	printf("       %s sm-notify mon_name ip_addr state\n", progname);
	exit(1);
}

/**
 * @brief To be used as the statd ha-callout program
 *
 * Examples
 *         progname add-client 192.168.10.94 nsds2
 *         progname del-client 192.168.10.94 nsds2
 *         progname sm-notify sitar1 192.168.10.94 127
 *
 * @param[in] event     One of add-client, del-client, sm-notify
 * @param[in] mon_name  The client being monitored.  For add-client, del-client
 *                      this will be the IP address. For sm-notify it will
 *                      be the hostname
 * @param[in] other     We don't actually use this.  For add-client and
 *                      del-client it will be NFS server hostname.
 *			For sm-notify, it will be the client's state number.
 */
int main(int argc, const char *argv[])
{
	const char *event = NULL;
	const char *mon_name = NULL;

	progname = argv[0];
	if (argc < 3) {
		usage();
	}

	read_config();

	event = argv[1];
	if (strcmp(event, "add-client") == 0) {
		mon_name = argv[2];
		switch (config.mode) {
		case CTDB_SC_MODE_PERSISTENT_DB:
			add_client_persistent_db(mon_name);
			break;
		case CTDB_SC_MODE_SHARED_DIR:
			add_client_shared_dir(mon_name);
			break;
		case CTDB_SC_MODE_NONE:
			break;
		}
	} else if (strcmp(event, "del-client") == 0) {
		mon_name = argv[2];
		switch (config.mode) {
		case CTDB_SC_MODE_PERSISTENT_DB:
			del_client_persistent_db(mon_name);
			break;
		case CTDB_SC_MODE_SHARED_DIR:
			del_client_shared_dir(mon_name);
			break;
		case CTDB_SC_MODE_NONE:
			break;
		}
	} else if (strcmp(event, "sm-notify") == 0) {
		exit(0);
	} else {
		usage();
	}

	free_config();
}