summaryrefslogtreecommitdiffstats
path: root/agents/virt/server/main.c
blob: fe5789981e366e848126b67c198866d1eae79682 (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
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/param.h>
#include <libgen.h>
#include <stdint.h>
#include <syslog.h>

/* Local includes */
#include "simpleconfig.h"
#include "static_map.h"
#include "xvm.h"
#include "server_plugin.h"
#include "simple_auth.h"
#include "debug.h"

/* configure.c */
int daemon_init(const char *prog, const char *pid_file, int nofork);
int daemon_cleanup(void);


static void
usage(void)
{
	printf("Usage: fence_virtd [options]\n");
	printf("  -F               Do not daemonize.\n");
	printf("  -f <file>        Use <file> as configuration file.\n");
	printf("  -d <level>       Set debugging level to <level>.\n");
	printf("  -c               Configuration mode.\n");
	printf("  -l               List plugins.\n");
	printf("  -w               Wait for initialization.\n");
	printf("  -p <file>        Use <file> to record the active process id.\n");
}


static int run = 1;
static void
exit_handler(int sig)
{
	run = 0;
}


int
main(int argc, char **argv)
{
	char val[4096];
	char listener_name[80];
	char backend_name[80];
	const char *config_file = SYSCONFDIR "/fence_virt.conf";
	char *pid_file = NULL;
	config_object_t *config = NULL;
	map_object_t *map = NULL;
	const listener_plugin_t *lp;
	const backend_plugin_t *p;
	listener_context_t listener_ctx = NULL;
	backend_context_t backend_ctx = NULL;
	int debug_set = 0, foreground = 0, wait_for_init = 0;
	int opt, configure = 0;

	config = sc_init();
	map = map_init();

	if (!config || !map) {
		perror("malloc");
		return -1;
	}

	while ((opt = getopt(argc, argv, "Ff:d:cwlhp:")) != EOF) {
		switch(opt) {
		case 'F':
			printf("Background mode disabled\n");
			foreground = 1;
			break;
		case 'f':
			printf("Using %s\n", optarg);
			config_file = optarg;
			break;
		case 'p':
			printf("Using %s\n", optarg);
			pid_file = optarg;
			break;
		case 'd':
			debug_set = atoi(optarg);
			break;
		case 'c':
			configure = 1;
			break;
		case 'w':
			wait_for_init = 1;
			break;
		case 'l':
			plugin_dump();
			return 0;
		case 'h':
		case '?':
			usage();
			return 0;
		default:
			return -1;
		}
	}

	if (configure) {
		return do_configure(config, config_file);
	}

	if (sc_parse(config, config_file) != 0) {
		printf("Failed to parse %s\n", config_file);
		return -1;
	}

	if (debug_set) {
		snprintf(val, sizeof(val), "%d", debug_set);
		sc_set(config, "fence_virtd/@debug", val);
	} else {
		if (sc_get(config, "fence_virtd/@debug", val, sizeof(val))==0)
			debug_set = atoi(val);
	}

	dset(debug_set);

	if (!foreground) {
		if (sc_get(config, "fence_virtd/@foreground",
			   val, sizeof(val)) == 0)
			foreground = atoi(val);
	}

	if (!wait_for_init) {
		if (sc_get(config, "fence_virtd/@wait_for_init",
			   val, sizeof(val)) == 0)
			wait_for_init = atoi(val);
		if (!wait_for_init) {
			/* XXX compat */
			if (sc_get(config, "fence_virtd/@wait_for_backend",
				   val, sizeof(val)) == 0)
				wait_for_init = atoi(val);
		}
	}

	if (dget() > 3)
		sc_dump(config, stdout);

	if (sc_get(config, "fence_virtd/@backend", backend_name,
		   sizeof(backend_name))) {
		printf("Failed to determine backend.\n");
		printf("%s\n", val);
		return -1;
	}

	dbg_printf(1, "Backend plugin: %s\n", backend_name);

	if (sc_get(config, "fence_virtd/@listener", listener_name,
		   sizeof(listener_name))) {
		printf("Failed to determine backend.\n");
		printf("%s\n", val);
		return -1;
	}

	dbg_printf(1, "Listener plugin: %s\n", listener_name);

	if (sc_get(config, "fence_virtd/@module_path", val,
		   sizeof(val))) {
#ifdef MODULE_PATH
		snprintf(val, sizeof(val), MODULE_PATH);
#else
		printf("Failed to determine module path.\n");
		return -1;
#endif
	}

	dbg_printf(1, "Searching %s for plugins...\n", val);

	opt = plugin_search(val);
	if (opt > 0) {
		dbg_printf(1, "%d plugins found\n", opt);
	} else {
		printf("No plugins found\n");
		return 1;
	}

	if (dget() > 3)
		plugin_dump();

	lp = plugin_find_listener(listener_name);
	if (!lp) {
		printf("Could not find listener \"%s\"\n", listener_name);
		return 1;
	}

	p = plugin_find_backend(backend_name);
	if (!p) {
		printf("Could not find backend \"%s\"\n", backend_name);
		return 1;
	}

	if (pid_file == NULL) {
		pid_file = malloc(PATH_MAX);
		memset(pid_file, 0, PATH_MAX);
		snprintf(pid_file, PATH_MAX, "/var/run/%s.pid", basename(argv[0]));
	}

	if (check_file_permissions(config_file) != 0)
		return -1;

	sprintf(val, "listeners/%s/@key_file", listener_name);
	if (sc_get(config, val,
		   val, sizeof(val)-1) == 0) {
		dbg_printf(1, "Got %s for key_file\n", val);
	} else {
		snprintf(val, sizeof(val), "%s", DEFAULT_KEY_FILE);
	}

	if (check_file_permissions(val) != 0)
		return -1;

	openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);

	daemon_init(basename(argv[0]), pid_file, foreground);

	signal(SIGINT, exit_handler);
	signal(SIGTERM, exit_handler);
	signal(SIGQUIT, exit_handler);

	syslog(LOG_NOTICE, "fence_virtd starting.  Listener: %s  Backend: %s",
		listener_name, backend_name);

	while (p->init(&backend_ctx, config) < 0) {
		if (!wait_for_init || !run) {
			if (foreground) {
				printf("Backend plugin %s failed to initialize\n",
				       backend_name);
			}
			syslog(LOG_ERR,
			       "Backend plugin %s failed to initialize\n",
			       backend_name);
			return 1;
		}
		sleep(5);
	}

	if (map_load(map, config) < 0) {
		syslog(LOG_WARNING, "Failed to load static maps\n");
	}

	/* only client we have now is mcast (fence_xvm behavior) */
	while (lp->init(&listener_ctx, p->callbacks, config, map,
			backend_ctx) != 0) {
		if (!wait_for_init || !run) {
			if (foreground) {
				printf("Listener plugin %s failed to initialize\n",
				       listener_name);
			}
			syslog(LOG_ERR,
			       "Listener plugin %s failed to initialize\n",
			       listener_name);
			return 1;
		}
		sleep(5);
	}

	while (run && lp->dispatch(listener_ctx, NULL) >= 0);

	syslog(LOG_NOTICE, "fence_virtd shutting down");

	map_release(map);
	sc_release(config);

	lp->cleanup(listener_ctx);
	p->cleanup(backend_ctx);

	plugin_unload();
	daemon_cleanup();

	return 0;
}