Commit 609b0b59 authored by Leigh Stoller's avatar Leigh 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 487c7e04
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
# Copyright (c) 2000-2004, 2006 University of Utah and the Flux Group.
# All rights reserved.
#
......@@ -32,13 +32,14 @@ suexec: suexec.c suexec.h
-o suexec $<
genlastlog: genlastlog.c
$(CC) $(CFLAGS) -I/usr/local/include \
$(CC) $(CFLAGS) -I/usr/local/include -I$(TESTBED_SRCDIR)/lib/libtb \
-DTBDBNAME='"$(TBDBNAME)"' \
-DUSERSVAR='"$(prefix)/usersvar"' \
-DOURDOMAIN='"$(OURDOMAIN)"' \
-DUSERNODE='"$(USERNODE)"' \
-DLOG_TESTBED=$(LOG_TESTBED) \
-o genlastlog $< -L/usr/local/lib/mysql -lmysqlclient
-o genlastlog $< ${OBJDIR}/lib/libtb/libtb.a \
-L/usr/local/lib/mysql -lmysqlclient
install: $(addprefix $(INSTALL_LIBEXECDIR)/, $(BINS)) \
$(addprefix $(INSTALL_SBINDIR)/, $(SBINS))
......
/*
* EMULAB-COPYRIGHT
* Copyright (c) 2000-2003 University of Utah and the Flux Group.
* Copyright (c) 2000-2003, 2006 University of Utah and the Flux Group.
* All rights reserved.
*/
......@@ -60,6 +60,7 @@
#include <sys/socket.h>
#include <mysql/mysql.h>
#include <zlib.h>
#include "tbdb.h"
/*
* This is the NFS mountpoint where users:/var is mounted.
......@@ -77,7 +78,6 @@
static char *progname;
static int debug = 0;
static int mydb_update(char *query, ...);
static int doit(gzFile *infp);
static char opshostname[MAXHOSTNAMELEN];
static jmp_buf deadline;
......@@ -128,6 +128,11 @@ main(int argc, char **argv)
openlog("genlastlog", LOG_PID, LOG_TESTBED);
syslog(LOG_NOTICE, "genlastlog starting");
if (!dbinit()) {
syslog(LOG_ERR, "Could not connect to DB!");
exit(1);
}
/*
* We need the canonical hostname for the usersnode so that we can
* put those logins in another table.
......@@ -315,7 +320,7 @@ doit(gzFile *infp)
"values ('%s', '%s', "
" FROM_UNIXTIME(%ld, '%%Y-%%m-%%d'), "
" FROM_UNIXTIME(%ld, '%%T')) ",
user, node, ll_time, ll_time) < 0)
user, node, ll_time, ll_time) == 0)
break;
if (strncmp(node, opshostname, strlen(node)) == 0 ||
......@@ -325,7 +330,7 @@ doit(gzFile *infp)
"values ('%s', "
" FROM_UNIXTIME(%ld, '%%Y-%%m-%%d'), "
" FROM_UNIXTIME(%ld, '%%T')) ",
user, ll_time, ll_time) < 0)
user, ll_time, ll_time) == 0)
break;
}
else {
......@@ -334,50 +339,9 @@ doit(gzFile *infp)
"values ('%s', '%s', "
" FROM_UNIXTIME(%ld, '%%Y-%%m-%%d'), "
" FROM_UNIXTIME(%ld, '%%T')) ",
node, user, ll_time, ll_time) < 0)
node, user, ll_time, ll_time) == 0)
break;
}
}
return 0;
}
static int
mydb_update(char *query, ...)
{
static MYSQL db;
static int inited;
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;
}
if (debug) {
printf("%s\n", querybuf);
return 0;
}
if (! inited) {
mysql_init(&db);
if (mysql_real_connect(&db, 0, "genlastlog",
0, TBDBNAME, 0, 0, 0) == 0) {
syslog(LOG_ERR, "%s: connect failed: %s",
TBDBNAME, mysql_error(&db));
return -1;
}
inited = 1;
}
if (mysql_real_query(&db, querybuf, n) != 0) {
syslog(LOG_ERR, "%s: query failed: %s",
TBDBNAME, mysql_error(&db));
return -1;
}
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