bootinfo_mysql.c 7.76 KB
Newer Older
1
2
#include <sys/types.h>
#include <netinet/in.h>
3
#include <netdb.h>
4
#include <stdio.h>
5
#include <syslog.h>
6

7
#include "bootwhat.h"
8
9
10
11
#include <mysql/mysql.h>

#ifdef USE_MYSQL_DB

12
static char dbname[] = TBDBNAME;
13
static MYSQL db;
14
static int parse_multiboot_path(char *path, boot_what_t *info);
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

int
open_bootinfo_db(void)
{
	return 0;
}

int
query_bootinfo_db(struct in_addr ipaddr, boot_what_t *info)
{
	char querybuf[1024];
	int n, nrows, ncols, part;
	MYSQL db;
	MYSQL_RES *res;
	MYSQL_ROW row;
30
31
	char dbquery[] =
		"select n.next_boot_path, n.next_boot_cmd_line, "
32
33
		"n.def_boot_osid, p.partition, n.def_boot_cmd_line, "
	        "n.def_boot_path, o.path from nodes "
34
		"as n left join partitions as p on n.node_id=p.node_id and "
35
		"n.def_boot_osid=p.osid left join interfaces as i on "
36
37
38
		"i.node_id=n.node_id "
    	        "left join os_info as o on o.osid=n.def_boot_osid "
	        "where i.IP = '%s'";
39
40
41

#define NEXT_BOOT_PATH		0
#define NEXT_BOOT_CMD_LINE	1
42
#define DEF_BOOT_OSID		2
43
44
45
#define PARTITION		3
#define DEF_BOOT_CMD_LINE	4
#define DEF_BOOT_PATH		5
46
#define OSID_PATH		6
47
48
49

	n = snprintf(querybuf, sizeof querybuf, dbquery, inet_ntoa(ipaddr));
	if (n > sizeof querybuf) {
50
		syslog(LOG_ERR, "query too long for buffer");
51
52
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
53
	
54
55
	mysql_init(&db);
	if (mysql_real_connect(&db, 0, 0, 0, dbname, 0, 0, 0) == 0) {
56
		syslog(LOG_ERR, "%s: connect failed: %s",
57
58
59
60
			dbname, mysql_error(&db));
		return 1;
	}

Mike Hibler's avatar
Mike Hibler committed
61
62
63
64
	/* Debug message into log:
	syslog(LOG_ERR, "USING QUERY: %s", querybuf);
	*/
	
65
	if (mysql_real_query(&db, querybuf, n) != 0) {
66
		syslog(LOG_ERR, "%s: query failed: %s",
67
68
69
70
71
72
73
			dbname, mysql_error(&db));
		mysql_close(&db);
		return 1;
	}

	res = mysql_store_result(&db);
	if (res == 0) {
74
		syslog(LOG_ERR, "%s: store_result failed: %s",
75
76
77
78
79
80
81
82
83
84
			dbname, mysql_error(&db));
		mysql_close(&db);
		return 1;

	}
	mysql_close(&db);

	nrows = (int)mysql_num_rows(res);
	switch (nrows) {
	case 0:
85
		syslog(LOG_ERR, "%s: no entry for host %s",
86
87
88
89
90
91
			dbname, inet_ntoa(ipaddr));
		mysql_free_result(res);
		return 1;
	case 1:
		break;
	default:
92
		syslog(LOG_ERR, "%s: %d entries for IP %s, using first",
93
94
95
96
97
98
			dbname, nrows, inet_ntoa(ipaddr));
		break;
	}

	ncols = (int)mysql_num_fields(res);
	switch (ncols) {
99
	case 7: /* Should have 7 fields */
100
101
		break;
	default:
102
		syslog(LOG_ERR, "%s: %d fields in query for IP %s!",
103
104
105
106
107
108
			dbname, ncols, inet_ntoa(ipaddr));
		mysql_free_result(res);
		return 1;
	}

	row = mysql_fetch_row(res);
Mike Hibler's avatar
Mike Hibler committed
109
110

	/*
111
	 * Check next_boot_path.  If set, assume it is a multiboot kernel.
Mike Hibler's avatar
Mike Hibler committed
112
	 */
113
	if (row[NEXT_BOOT_PATH] != 0 && row[NEXT_BOOT_PATH][0] != '\0') {
Mike Hibler's avatar
Mike Hibler committed
114
		info->type = BIBOOTWHAT_TYPE_MB;
115
116
		parse_multiboot_path(row[NEXT_BOOT_PATH], info);

117
118
119
120
		if (row[NEXT_BOOT_CMD_LINE] != 0 &&
		    row[NEXT_BOOT_CMD_LINE][0] != '\0')
			strncpy(info->cmdline, row[NEXT_BOOT_CMD_LINE],
				MAX_BOOT_CMDLINE-1);
121
122
		else
			info->cmdline[0] = 0;	/* Must zero first byte! */
Mike Hibler's avatar
Mike Hibler committed
123
124
125
126
		mysql_free_result(res);
		return 0;
	}

127
128
129
130
	/*
	 * There is either a partition number or default boot path.
	 * The default boot path overrides the partition.
	 */
131
	if (row[DEF_BOOT_PATH] != 0 && row[DEF_BOOT_PATH][0] != '\0') {
132
		info->type = BIBOOTWHAT_TYPE_MB;
133
		parse_multiboot_path(row[DEF_BOOT_PATH], info);
134
	}
135
136
	else if (row[OSID_PATH] != 0 && row[OSID_PATH][0] != '\0') {
		info->type = BIBOOTWHAT_TYPE_MB;
137
		parse_multiboot_path(row[DEF_BOOT_PATH], info);
138
	}
139
	else if (row[PARTITION] != 0 && row[PARTITION][0] != '\0') {
140
		info->type = BIBOOTWHAT_TYPE_PART;
141
		info->what.partition = atoi(row[PARTITION]);
142
143
	}
	else {
144
		syslog(LOG_ERR, "%s: null query result for IP %s!",
145
146
147
148
			dbname, inet_ntoa(ipaddr));
		mysql_free_result(res);
		return 1;
	}
149
150
151
	if (row[DEF_BOOT_CMD_LINE] != 0 && row[DEF_BOOT_CMD_LINE][0] != '\0')
		strncpy(info->cmdline, row[DEF_BOOT_CMD_LINE],
			MAX_BOOT_CMDLINE-1);
152
153
154
	else
		info->cmdline[0] = 0;	/* Must zero first byte! */
	
155
156
157
158
159
	/*
	 * Just 45040 lines of code later, we are done. 
	 */
	mysql_free_result(res);
	return 0;
160
161
162

#undef NEXT_BOOT_PATH
#undef NEXT_BOOT_CMD_LINE
163
#undef DEF_BOOT_OSID
164
165
166
#undef PARTITION
#undef DEF_BOOT_CMD_LINE
#undef DEF_BOOT_PATH
167
#undef OSID_PATH
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
}

int
ack_bootinfo_db(struct in_addr ipaddr, boot_what_t *info)
{
	char querybuf[1024];
	int n, nrows, ncols, part;
	MYSQL db;
	MYSQL_RES *res;
	MYSQL_ROW row;

	n = snprintf(querybuf, sizeof querybuf,
		     "select i.node_id,n.next_boot_path,n.next_boot_cmd_line "
		     "from nodes as n left join interfaces as i on "
		     "i.node_id=n.node_id where i.IP = '%s'",
		     inet_ntoa(ipaddr));
	if (n > sizeof querybuf) {
		syslog(LOG_ERR, "query too long for buffer");
		return 1;
	}
	
	mysql_init(&db);
	if (mysql_real_connect(&db, 0, 0, 0, dbname, 0, 0, 0) == 0) {
		syslog(LOG_ERR, "%s: connect failed: %s",
			dbname, mysql_error(&db));
		return 1;
	}

	if (mysql_real_query(&db, querybuf, n) != 0) {
		syslog(LOG_ERR, "%s: query failed: %s",
			dbname, mysql_error(&db));
		mysql_close(&db);
		return 1;
	}

	res = mysql_store_result(&db);
	if (res == 0) {
		syslog(LOG_ERR, "%s: store_result failed: %s",
			dbname, mysql_error(&db));
		mysql_close(&db);
		return 1;

	}

	nrows = (int)mysql_num_rows(res);
	switch (nrows) {
	case 0:
		syslog(LOG_ERR, "%s: no entry for host %s",
			dbname, inet_ntoa(ipaddr));
		mysql_free_result(res);
		return 1;
	case 1:
		break;
	default:
		syslog(LOG_ERR, "%s: %d entries for IP %s, using first",
			dbname, nrows, inet_ntoa(ipaddr));
		break;
	}

	row = mysql_fetch_row(res);

	if (row[1] == 0 || row[1][0] == '\0') {
		/* Nothing to do */
		mysql_free_result(res);
		mysql_close(&db);
		return 1;
	}
	
	/*
	 * Update the database to reflect that the boot has been done.
	 */
	n = snprintf(querybuf, sizeof querybuf,
		     "update nodes set next_boot_path='',"
		     "next_boot_cmd_line='' where node_id='%s'",
		     row[0]);
	mysql_free_result(res);
	
	if (n > sizeof querybuf) {
		syslog(LOG_ERR, "query too long for buffer");
		mysql_close(&db);
		return 1;
	}
	
	if (mysql_real_query(&db, querybuf, n) != 0) {
		syslog(LOG_ERR, "%s: query failed: %s",
			dbname, mysql_error(&db));
		mysql_close(&db);
		return 1;
	}
	syslog(LOG_INFO, "%s: next_boot_path cleared", inet_ntoa(ipaddr));

	mysql_close(&db);
260
261
262
263
264
265
266
267
268
	return 0;
}

int
close_bootinfo_db(void)
{
	return 0;
}

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
/*
 * Split a multiboot path into the IP and Path.
 */
static int
parse_multiboot_path(char *path, boot_what_t *info)
{
	char		*p  = path;
	struct hostent	*he;

	info->type = BIBOOTWHAT_TYPE_MB;
	info->what.mb.tftp_ip.s_addr = 0;

	strsep(&p, ":");
	if (p) {
		he = gethostbyname(path);
		path = p;
	}
	else {
		he = gethostbyname("users.emulab.net");
	}
	if (he) {
		memcpy((char *)&info->what.mb.tftp_ip,
		       he->h_addr, sizeof(info->what.mb.tftp_ip));
	}

	strncpy(info->what.mb.filename, path, MAX_BOOT_PATH-1);

	return 0;
}

299
#ifdef TEST
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdarg.h>

void
syslog(int prio, const char *msg, ...)
{
	va_list ap;

	va_start(ap, msg);
	vfprintf(stderr, msg, ap);
	va_end(ap);
	fprintf(stderr, "\n");
}

Mike Hibler's avatar
Mike Hibler committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
static void
print_bootwhat(boot_what_t *bootinfo)
{
	switch (bootinfo->type) {
	case BIBOOTWHAT_TYPE_PART:
		printf("boot from partition %d\n",
		       bootinfo->what.partition);
		break;
	case BIBOOTWHAT_TYPE_SYSID:
		printf("boot from partition with sysid %d\n",
		       bootinfo->what.sysid);
		break;
	case BIBOOTWHAT_TYPE_MB:
		printf("boot multiboot image %s:%s\n",
		       inet_ntoa(bootinfo->what.mb.tftp_ip),
		       bootinfo->what.mb.filename);
		break;
	}
331
332
333
	if (bootinfo->cmdline[0])
		printf("Command line %s\n", bootinfo->cmdline);
		
Mike Hibler's avatar
Mike Hibler committed
334
335
}

336
337
338
339
340
341
main(int argc, char **argv)
{
	struct in_addr ipaddr;
	boot_info_t boot_info;
	boot_what_t *boot_whatp = (boot_what_t *)&boot_info.data;

342
	open_bootinfo_db();
343
344
	while (--argc > 0) {
		if (inet_aton(*++argv, &ipaddr))
Mike Hibler's avatar
Mike Hibler committed
345
346
347
348
			if (query_bootinfo_db(ipaddr, boot_whatp) == 0) {
				printf("%s: ", *argv);
				print_bootwhat(boot_whatp);
			} else
349
				printf("%s: failed\n", *argv);
350
351
352
		else
			printf("bogus IP address `%s'\n", *argv);
	}
353
	close_bootinfo_db();
354
355
356
357
358
	exit(0);
}
#endif

#endif