Skip to content

Commit

Permalink
Overhaul config parsing
Browse files Browse the repository at this point in the history
Use libucl, centralize parsing, add basic redis configurability.
  • Loading branch information
flowerysong committed Dec 9, 2016
1 parent 3ba7fcb commit a85eb14
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 233 deletions.
8 changes: 4 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ AM_CPPFLAGS = @CPPFLAGS@ @LDAP_CPPFLAGS@ @BDB_CPPFLAGS@

bin_PROGRAMS = simvacation simunvacation

simvacation_SOURCES = yasl.c yasl.h argcargv.c argcargv.h vdb.h vlu.h simvacation.h simvacation.c
simvacation_LDADD =
simvacation_SOURCES = yasl.c yasl.h vdb.h vlu.h vutil.c vutil.h simvacation.h simvacation.c
simvacation_LDADD = $(LIBUCL_LIBS)

simunvacation_SOURCES = yasl.c yasl.h argcargv.c argcargv.h simvacation.h vdb.h vlu.h simunvacation.c
simunvacation_LDADD =
simunvacation_SOURCES = yasl.c yasl.h simvacation.h vdb.h vlu.h vutil.c vutil.h simunvacation.c
simunvacation_LDADD = $(LIBUCL_LIBS)

if BACKEND_NULL
simvacation_SOURCES += vdb_null.c vdb_null.h vlu_null.c vlu_null.h
Expand Down
119 changes: 0 additions & 119 deletions argcargv.c

This file was deleted.

11 changes: 0 additions & 11 deletions argcargv.h

This file was deleted.

2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ AC_PROG_CC
AC_PROG_CC_C99

# Checks for libraries.
PKG_CHECK_MODULES([LIBUCL], [libucl])

AS_IF([test x$with_backend = xbdb],
[AX_PATH_BDB([4], [
AC_DEFINE([HAVE_BDB], [1], [Define if BDB exists.])
Expand Down
1 change: 1 addition & 0 deletions packaging/rpm/simvacation.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ URL: http://its.umich.edu/
Source0: %{name}-%{version}.tar.xz
BuildRequires: setup
BuildRequires: pkgconfig(urcl)
BuildRequires: pkgconfig(libucl)
BuildRequires: openldap-devel

%description
Expand Down
17 changes: 12 additions & 5 deletions simunvacation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* simunvacation.c - clean up database files for people who are no longer
* "on vacation".
*
* Copyright (c) 2004-2015 Regents of The University of Michigan.
* Copyright (c) 2004-2016 Regents of The University of Michigan.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,6 +36,7 @@
#include "simvacation.h"
#include "vdb.h"
#include "vlu.h"
#include "vutil.h"

void usage( void );

Expand All @@ -48,7 +49,9 @@ int main( int argc, char **argv)

struct vlu *vlu;
struct vdb *vdb;
char *vlu_config = CONFFILE;

char *config_file = CONFFILE;
ucl_object_t *config;

struct name_list *uniqnames;
struct name_list *u;
Expand All @@ -57,7 +60,7 @@ int main( int argc, char **argv)
switch( (char) ch ) {

case 'c':
vlu_config = optarg;
config_file = optarg;
break;

case 'd':
Expand All @@ -77,10 +80,14 @@ int main( int argc, char **argv)
openlog( "simunvacation", LOG_PID, LOG_VACATION );
}

vdb = vdb_init( "simunvacation" );
if (( config = vacation_config( config_file )) == NULL ) {
exit( 1 );
}

vdb = vdb_init( config, "simunvacation" );
uniqnames = vdb_get_names( vdb );

vlu = vlu_init( vlu_config );
vlu = vlu_init( config );
if ( vlu_connect( vlu ) != 0 ) {
vdb_close( vdb );
exit( 1 );
Expand Down
24 changes: 16 additions & 8 deletions simvacation.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
#include <strings.h>
#include <unistd.h>

#include <ucl.h>

#include "simvacation.h"
#include "vdb.h"
#include "vlu.h"
#include "vutil.h"

/*
* VACATION -- return a message to the sender when on vacation.
Expand Down Expand Up @@ -92,11 +95,12 @@ int debug;
main( int argc, char **argv )
{
debug = 0;
time_t interval;
int ch, rc;
char *vacmsg;
char *progname;
char *vlu_config = CONFFILE;
time_t interval;
int ch, rc;
char *vacmsg;
char *progname;
char *config_file = CONFFILE;
ucl_object_t *config;

if ( (progname = strrchr( argv[0], '/' )) == NULL )
progname = strdup( argv[0] );
Expand All @@ -115,7 +119,7 @@ main( int argc, char **argv )
while (( ch = getopt( argc, argv, "c:df:r:" )) != EOF ) {
switch( (char) ch ) {
case 'c':
vlu_config = optarg;
config_file = optarg;
break;
case 'd':
debug = 1;
Expand Down Expand Up @@ -154,7 +158,11 @@ main( int argc, char **argv )

rcpt = *argv;

if (( vlu = vlu_init( vlu_config )) == NULL ) {
if (( config = vacation_config( config_file )) == NULL ) {
myexit( EX_TEMPFAIL );
}

if (( vlu = vlu_init( config )) == NULL ) {
myexit( EX_TEMPFAIL );
}

Expand All @@ -175,7 +183,7 @@ main( int argc, char **argv )
myexit( rc );
}

if (( vdb = vdb_init( rcpt )) == NULL ) {
if (( vdb = vdb_init( config, rcpt )) == NULL ) {
myexit( EX_OK );
}

Expand Down
4 changes: 3 additions & 1 deletion vdb.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef BACKEND_VDB_H
#define BACKEND_VDB_H

struct vdb *vdb_init( char * );
#include <ucl.h>

struct vdb *vdb_init( const ucl_object_t *, const char * );
void vdb_close( struct vdb * );
int vdb_recent( struct vdb *, char * );
int vdb_store_interval( struct vdb *, time_t );
Expand Down
2 changes: 1 addition & 1 deletion vdb_berkeley.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void bdb_err_log ( const DB_ENV *, const char *, const char * );
char *bdb_path( char *, char * );

struct vdb *
vdb_init( char *rcpt )
vdb_init( const ucl_object_t *config, const char *rcpt )
{
int rc;
char *path;
Expand Down
2 changes: 1 addition & 1 deletion vdb_lmdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ char *lmdb_vdb_key( char *, char * );
void lmdb_vdb_assert( MDB_env *, const char * );

struct vdb *
vdb_init( char *rcpt )
vdb_init( const ucl_object_t *config, const char *rcpt )
{
int rc;
struct vdb *vdb;
Expand Down
4 changes: 2 additions & 2 deletions vdb_null.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2015 Regents of The University of Michigan
* Copyright (c) 2014-2016 Regents of The University of Michigan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -27,7 +27,7 @@
#include "vdb_null.h"

struct vdb *
vdb_init( char *rcpt )
vdb_init( ucl_object_t *config, char *rcpt )
{
return( calloc( 1, 1 ));
}
Expand Down
38 changes: 31 additions & 7 deletions vdb_redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,51 @@
static char *redis_vdb_key( char *, char * );

struct vdb *
vdb_init( char *rcpt )
vdb_init( const ucl_object_t *config, const char *rcpt )
{
struct vdb *vdb;
struct vdb *vdb;
struct vdb *res = NULL;
const char *host = NULL;
int64_t port = 6379;
const ucl_object_t *config_key;

if (( config = ucl_object_lookup( config, "redis" )) == NULL ) {
syslog( LOG_DEBUG, "vdb_init: no config information, using defaults" );
} else {
if (( config_key = ucl_object_lookup( config, "host" )) != NULL ) {
if ( !ucl_object_tostring_safe( config_key, &host )) {
syslog( LOG_ERR, "vdb_init: ucl_object_tostring_safe failed" );
goto error;
}
}
if (( config_key = ucl_object_lookup( config, "port" )) != NULL ) {
if ( !ucl_object_toint_safe( config_key, &port )) {
syslog( LOG_ERR, "vdb_init: ucl_object_toint_safe failed" );
goto error;
}
}
}

if (( vdb = calloc( 1, sizeof( struct vdb ))) == NULL ) {
return( NULL );
goto error;
}

if (( vdb->u = urcl_connect( VDBDIR, 6379 )) == NULL ) {
if (( vdb->u = urcl_connect( host ? host : VDBDIR , port )) == NULL ) {
syslog( LOG_ALERT, "redis vdb_init urcl_connect: failed" );
goto error;
}

vdb->interval = SECSPERDAY * DAYSPERWEEK;
vdb->rcpt = strdup( rcpt );

return( vdb );
res = vdb;

error:
vdb_close( vdb );
return( NULL );
if ( vdb && !res ) {
vdb_close( vdb );
}

return( res );
}

void
Expand Down
4 changes: 3 additions & 1 deletion vlu.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef VLU_H
#define VLU_H

#include <ucl.h>

#define VLU_RESULT_OK 0
#define VLU_RESULT_TEMPFAIL 1
#define VLU_RESULT_PERMFAIL 2

struct vlu *vlu_init( char * );
struct vlu *vlu_init( const ucl_object_t * );
int vlu_connect( struct vlu * );
int vlu_search( struct vlu *, char * );
char *vlu_message( struct vlu *, char * );
Expand Down
Loading

0 comments on commit a85eb14

Please sign in to comment.