Commit 487c7e04 authored by Leigh B. Stoller's avatar Leigh B. Stoller

Convert to using libtb instead of hand rolled mysql query interface.

Most programs already use that library; getting the stragglers before
I whack the library for mysql 5.X
parent aa5628b8
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
# All rights reserved.
#
......@@ -20,7 +20,7 @@ client: capture capquery
include $(TESTBED_SRCDIR)/GNUmakerules
DBFLAGS += -I/usr/local/include
DBFLAGS += -I/usr/local/include -I$(TESTBED_SRCDIR)/lib/libtb
#
# Define LOG_DROPS to record warnings in syslog whenever chars were dropped
......@@ -52,6 +52,7 @@ capture-tty: capture.c capdecls.h
capserver: capserver.c capdecls.h
$(CC) $(CFLAGS) $(DBFLAGS) -o capserver $< \
${OBJDIR}/lib/libtb/libtb.a \
-L/usr/local/lib/mysql -lmysqlclient
#
......
/*
* EMULAB-COPYRIGHT
* Copyright (c) 2000-2003, 2005 University of Utah and the Flux Group.
* Copyright (c) 2000-2003, 2005, 2006 University of Utah and the Flux Group.
* All rights reserved.
*/
......@@ -21,19 +21,13 @@
#include <grp.h>
#include "capdecls.h"
#include "config.h"
#include "tbdb.h"
#define TESTMODE
/* Defined in configure and passed in via the makefile */
#define DBNAME_SIZE 64
#define DEFAULT_DBNAME TBDBNAME
static int debug = 0;
static int portnum = SERVERPORT;
static char *dbname = DEFAULT_DBNAME;
static gid_t admingid;
MYSQL_RES * mydb_query(char *query, int ncols, ...);
int mydb_update(char *query, ...);
char *usagestr =
"usage: capserver [-d] [-p #]\n"
......@@ -84,6 +78,11 @@ main(int argc, char **argv)
if (!debug)
(void)daemon(0, 0);
if (!dbinit()) {
syslog(LOG_ERR, "Could not connect to DB!");
exit(1);
}
/*
* Grab the GID for the default group.
*/
......@@ -257,12 +256,12 @@ main(int argc, char **argv)
/*
* Update the DB.
*/
else if (mydb_update("update tiplines set portnum=%d, "
"keylen=%d, keydata='%s' "
"where tipname='%s'",
whoami.portnum,
whoami.key.keylen, whoami.key.key,
whoami.name)) {
else if (! mydb_update("update tiplines set portnum=%d, "
"keylen=%d, keydata='%s' "
"where tipname='%s'",
whoami.portnum,
whoami.key.keylen, whoami.key.key,
whoami.name)) {
syslog(LOG_ERR, "DB Error updating tiplines for %s!",
whoami.name);
goto done;
......@@ -294,87 +293,3 @@ main(int argc, char **argv)
exit(0);
}
/*
* DB stuff
*/
MYSQL_RES *
mydb_query(char *query, int ncols, ...)
{
MYSQL db;
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)) {
syslog(LOG_ERR, "query too long for buffer");
return (MYSQL_RES *) 0;
}
mysql_init(&db);
if (mysql_real_connect(&db, 0, "capserver", 0, dbname, 0, 0, 0) == 0) {
syslog(LOG_ERR, "%s: connect failed: %s",
dbname, mysql_error(&db));
return (MYSQL_RES *) 0;
}
if (mysql_real_query(&db, querybuf, n) != 0) {
syslog(LOG_ERR, "%s: query failed: %s",
dbname, mysql_error(&db));
mysql_close(&db);
return (MYSQL_RES *) 0;
}
res = mysql_store_result(&db);
if (res == 0) {
syslog(LOG_ERR, "%s: store_result failed: %s",
dbname, mysql_error(&db));
mysql_close(&db);
return (MYSQL_RES *) 0;
}
mysql_close(&db);
if (ncols && ncols != (int)mysql_num_fields(res)) {
syslog(LOG_ERR, "%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, ...)
{
MYSQL db;
char querybuf[BUFSIZ];
va_list ap;
int n;
va_start(ap, query);
n = vsnprintf(querybuf, sizeof(querybuf), query, ap);
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;
}
mysql_close(&db);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment