tbdb.c 5.96 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
3
4
5
6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

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
/*
 * DB interface.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <assert.h>
#include "tbdb.h"
#include "log.h"
#include "config.h"

/*
 * DB stuff
 */
static MYSQL	db;
static char    *dbname = TBDBNAME;

int
dbinit(void)
{
	mysql_init(&db);
	if (mysql_real_connect(&db, 0, 0, 0,
			       dbname, 0, 0, CLIENT_INTERACTIVE) == 0) {
		error("%s: connect failed: %s", dbname, mysql_error(&db));
		return 0;
	}
	return 1;
}

MYSQL_RES *
mydb_query(char *query, int ncols, ...)
{
	MYSQL_RES	*res;
	char		querybuf[2*BUFSIZ];
	va_list		ap;
	int		n;

	va_start(ap, ncols);
	n = vsnprintf(querybuf, sizeof(querybuf), query, ap);
	if (n > sizeof(querybuf)) {
		error("query too long for buffer");
		return (MYSQL_RES *) 0;
	}

	if (mysql_real_query(&db, querybuf, n) != 0) {
		error("%s: query failed: %s", dbname, mysql_error(&db));
		return (MYSQL_RES *) 0;
	}

	res = mysql_store_result(&db);
	if (res == 0) {
		error("%s: store_result failed: %s", dbname, mysql_error(&db));
		return (MYSQL_RES *) 0;
	}

	if (ncols && ncols != (int)mysql_num_fields(res)) {
		error("%s: Wrong number of fields returned "
		      "Wanted %d, Got %d",
		      dbname, ncols, (int)mysql_num_fields(res));
		mysql_free_result(res);
		return (MYSQL_RES *) 0;
	}
	return res;
}

int
mydb_update(char *query, ...)
{
	char		querybuf[2*BUFSIZ];
	va_list		ap;
	int		n;

	va_start(ap, query);
	n = vsnprintf(querybuf, sizeof(querybuf), query, ap);
	if (n > sizeof(querybuf)) {
		error("query too long for buffer");
		return 0;
	}
	if (mysql_real_query(&db, querybuf, n) != 0) {
		error("%s: query failed: %s", dbname, mysql_error(&db));
		return 0;
	}
	return 1;
}

/*
 * Map IP to node ID. 
 */
int
mydb_iptonodeid(char *ipaddr, char *bufp)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;

	res = mydb_query("select node_id from interfaces where IP='%s'",
			 1, ipaddr);
	if (!res) {
		error("iptonodeid: DB Error: %s", ipaddr);
		return 0;
	}

	if (! (int)mysql_num_rows(res)) {
		error("iptonodeid: No such nodeid: %s", ipaddr);
		mysql_free_result(res);
		return 0;
	}
	row = mysql_fetch_row(res);
	mysql_free_result(res);
120
121
122
123
124
125
	strcpy(bufp, row[0]);

	return 1;
}
 
/*
126
 * Map node ID to IP (control net interface for underlying physical node!).
127
128
129
130
131
132
133
 */
int
mydb_nodeidtoip(char *nodeid, char *bufp)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;

134
135
136
	res = mydb_query("select IP from nodes as n2 "
			 "left join nodes as n1 on n1.node_id=n2.phys_nodeid "
			 "left join node_types as nt on n1.type=nt.type "
137
			 "left join interfaces as i on "
138
139
			 "i.node_id=n1.node_id and i.iface=nt.control_iface "
			 "where n2.node_id='%s'", 1, nodeid);
140
141
142
143
144
145
146
147
148
149
150
151
152

	if (!res) {
		error("nodeidtoip: DB Error: %s", nodeid);
		return 0;
	}

	if (! (int)mysql_num_rows(res)) {
		error("nodeidtoip: No such nodeid: %s", nodeid);
		mysql_free_result(res);
		return 0;
	}
	row = mysql_fetch_row(res);
	mysql_free_result(res);
153
154
155
156
157
158
159
160
161
162
163
	strcpy(bufp, row[0]);

	return 1;
}
 
/*
 * Set the node event status.
 */
int
mydb_setnodeeventstate(char *nodeid, char *eventtype)
{
164
	if (! mydb_update("update nodes set eventstate='%s' "
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
			  "where node_id='%s'",
			  eventtype, nodeid)) {
		error("setnodestatus: DB Error: %s/%s!", nodeid, eventtype);
		return 0;
	}
	return 1;
}

/*
 * See if all nodes in an experiment are at the specified event state.
 * Return number of nodes not in the proper state.
 */
int
mydb_checkexptnodeeventstate(char *pid, char *eid,
			     char *eventtype, int *count)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;
	int		nrows;

185
186
	res = mydb_query("select eventstate from reserved "
			 "left join nodes on nodes.node_id=reserved.node_id "
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
			 "where reserved.pid='%s' and reserved.eid='%s' ",
			 1, pid, eid);
	
	if (!res) {
		error("checkexptnodeeventstate: DB Error: %s/%s/%s",
		      pid, eid, eventtype);
		return 0;
	}

	if (! (nrows = mysql_num_rows(res))) {
		error("checkexptnodeeventstate: No such experiment: %s/%s",
		      pid, eid);
		mysql_free_result(res);
		return 0;
	}

	*count = 0;
	while (nrows) {
		row = mysql_fetch_row(res);

		if (!row[0] || !row[0][0] || strcmp(row[0], eventtype))
			*count += 1;
		nrows--;
	}
	mysql_free_result(res);
	return 1;
}

/*
 * Set (or clear) event scheduler process ID. A zero is treated as
 * a request to clear it.
218
219
220
 *
 * NOTE: We want to fail if there is already a non-zero value in the DB
 * (although its okay to set it to zero no matter what). 
221
222
223
224
 */
int
mydb_seteventschedulerpid(char *pid, char *eid, int processid)
{
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
	MYSQL_RES	*res;
	MYSQL_ROW	row;
	int		retval = 1;

	if (! processid) {
		if (! mydb_update("update experiments set event_sched_pid=0 "
				  "where pid='%s' and eid='%s'",
				  pid, eid)) {
			error("seteventschedulerpid: DB Error(0): %s/%s!",
			      pid, eid);
			return 0;
		}
		return 1;
	}

	if (! mydb_update("lock tables experiments write")) {
		error("seteventschedulerpid: DB Error: locking table!");
		return 0;
	}

	res = mydb_query("select event_sched_pid from experiments "
			 "where pid='%s' and eid='%s'",
			 1, pid, eid);
	
	if (!res) {
		error("seteventschedulerpid: DB Error(1): %s/%s", pid, eid);
		return 0;
	}
	if (! mysql_num_rows(res)) {
		error("seteventschedulerpid: No such experiment: %s/%s",
		      pid, eid);
		retval = 0;
		goto done;
	}
	row = mysql_fetch_row(res);
	
	if (!row[0] || !row[0][0]) {
		error("seteventschedulerpid: Bad value for procid: %s/%s",
		      pid, eid);
		retval = 0;
		goto done;
	}
	if (atoi(row[0]) != 0) {
		info("seteventschedulerpid: A scheduler is running: %s/%s",
		     pid, eid);
		retval = 0;
		goto done;
	}
273
274
275
	if (! mydb_update("update experiments set event_sched_pid=%d "
			  "where pid='%s' and eid='%s'",
			  processid, pid, eid)) {
276
277
278
279
280
281
282
283
		error("seteventschedulerpid: DB Error(2): %s/%s!", pid, eid);
		retval = 0;
		goto done;
	}
	retval = 1;
  done:	
	if (! mydb_update("unlock tables")) {
		error("seteventschedulerpid: DB Error: unlocking table!");
284
285
		return 0;
	}
286
287
	mysql_free_result(res);
	return retval;
288
}