Skip to contentSkip to conversation

CLD: drop dependency on libevent from libcldc

PATCH Applied Pete Zaitcev 2 participants 3 messages Nov 28, 2009 – Nov 29, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 CLD: drop dependency on libevent from libcldc

Let's remove the dependency on libevent completely. The core CLD daemon did not use libeven for some time, but getting rid of it entirely required an implementation of some kind of common main loop for tests, which is what this patch does.

Since we were refactoring, we add a common header and library for tests, same as Chunk has.

This patch introduces an incompatibility, so Chunk and tabled must be rebuilt. This is because the layout of struct cldc_udp changes.

---
 configure.ac           |    4 -
 include/cldc.h         |    7 --
 lib/cldc-udp.c         |   27 --------
 pkg/cld.spec           |    2 
 test/.gitignore        |    1 
 test/Makefile.am       |    9 ++
 test/it-works.c        |   68 ++++++++++++++--------
 test/load-file-event.c |  108 +++++++++++++++--------------------
 test/lock-file-event.c |  118 ++++++++++++++++++++++-----------------
 test/save-file-event.c |  109 +++++++++++++++---------------------
 test/test.h            |   26 ++++++++
 test/timer.c           |   44 ++++++++++++++
 test/timer.h           |   18 +++++
 test/util.c            |   66 +++++++++++++++++++++
 tools/Makefile.am      |    2 
 15 files changed, 375 insertions(+), 234 deletions(-)
configure.ac +2 -2
index 26a1548..b1247b6 100644
@@ -78,9 +78,9 @@ AC_CHECK_LIB(db-4.7, db_create, DB4_LIBS=-ldb-4.7,
 	AC_CHECK_LIB(db-4.5, db_create, DB4_LIBS=-ldb-4.5,
 	AC_CHECK_LIB(db-4.4, db_create, DB4_LIBS=-ldb-4.4,
 	AC_CHECK_LIB(db-4.3, db_create, DB4_LIBS=-ldb-4.3, exit 1)))))
-AC_CHECK_LIB(event, event_base_new, EVENT_LIBS=-levent, exit 1)
 AC_CHECK_LIB(argp, argp_parse, ARGP_LIBS=-largp)
 AC_CHECK_LIB(crypto, MD5_Init, CRYPTO_LIBS=-lcrypto)
+AC_CHECK_LIB(resolv, ns_initparse, RESOLV_LIBS=-lresolv)
 
 dnl -------------------------------------
 dnl Checks for optional library functions
@@ -108,9 +108,9 @@ PKG_PROG_PKG_CONFIG()
 AM_PATH_GLIB_2_0(2.0.0,,,gthread)
 
 AC_SUBST(DB4_LIBS)
-AC_SUBST(EVENT_LIBS)
 AC_SUBST(ARGP_LIBS)
 AC_SUBST(CRYPTO_LIBS)
+AC_SUBST(RESOLV_LIBS)
 
 AC_CONFIG_FILES([
 	Makefile
include/cldc.h +0 -7
index 8c2bde5..9ae6dfe 100644
@@ -3,7 +3,6 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
-#include <event.h>
 #include <glib.h>
 #include <cld_msg.h>
 
@@ -130,8 +129,6 @@ struct cldc_udp {
 
 	int		fd;
 
-	struct event	timer_ev;
-
 	struct cldc_session *sess;
 
 	int		(*cb)(struct cldc_session *, void *);
@@ -202,10 +199,6 @@ extern int cldc_udp_receive_pkt(struct cldc_udp *udp);
 extern int cldc_udp_pkt_send(void *private,
 			  const void *addr, size_t addrlen,
 			  const void *buf, size_t buflen);
-extern bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs);
 
 /* cldc-dns */
 extern int cldc_getaddr(GList **host_list, const char *thishost, bool verbose,
lib/cldc-udp.c +0 -27
index b212643..0a8d0ad 100644
@@ -42,14 +42,6 @@ void cldc_udp_free(struct cldc_udp *udp)
 	free(udp);
 }
 
-static void cldc_udp_timer(int fd, short events, void *userdata)
-{
-	struct cldc_udp *udp = userdata;
-
-	if (udp->cb)
-		udp->cb(udp->sess, udp->cb_private);
-}
-
 int cldc_udp_new(const char *hostname, int port,
 		 struct cldc_udp **udp_out)
 {
@@ -100,8 +92,6 @@ int cldc_udp_new(const char *hostname, int port,
 
 	udp->fd = fd;
 
-	evtimer_set(&udp->timer_ev, cldc_udp_timer, udp);
-
 	freeaddrinfo(res);
 
 	*udp_out = udp;
@@ -149,20 +139,3 @@ int cldc_udp_pkt_send(void *private,
 	return 0;
 }
 
-bool cldc_levent_timer(void *private, bool add,
-		       int (*cb)(struct cldc_session *, void *),
-		       void *cb_private,
-		       time_t secs)
-{
-	struct cldc_udp *udp = private;
-	struct timeval tv = { secs, 0 };
-
-	if (add) {
-		udp->cb = cb;
-		udp->cb_private = cb_private;
-		return evtimer_add(&udp->timer_ev, &tv) == 0;
-	} else {
-		return evtimer_del(&udp->timer_ev) == 0;
-	}
-}
-
pkg/cld.spec +1 -1
index 3b93a0d..daeaa4a 100644
@@ -15,7 +15,7 @@ BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 Requires(post):		chkconfig
 Requires(preun):	chkconfig initscripts
 
-BuildRequires:	db4-devel libevent-devel glib2-devel doxygen openssl-devel
+BuildRequires:	db4-devel glib2-devel doxygen openssl-devel
 BuildRequires:	texlive-latex
 
 %description
test/.gitignore +1 -0
index 386e7b1..00a676c 100644
@@ -1,5 +1,6 @@
 
 .libs
+libtest.a
 it-works
 save-file-event
 load-file-event
test/Makefile.am +8 -1
index af67b90..0287127 100644
@@ -3,6 +3,8 @@ INCLUDES        = -I$(top_srcdir)/include       \
 		  @GLIB_CFLAGS@
 
 EXTRA_DIST =			\
+	test.h			\
+	timer.h			\
 	prep-db			\
 	start-daemon		\
 	pid-exists		\
@@ -24,10 +26,15 @@ check_PROGRAMS		= it-works \
 			  save-file-event load-file-event lock-file-event
 
 TESTLDADD		= ../lib/libcldc.la	\
-			  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@
+			  libtest.a		\
+			  @GLIB_LIBS@ @CRYPTO_LIBS@ @RESOLV_LIBS@
 it_works_LDADD		= $(TESTLDADD)
 save_file_event_LDADD	= $(TESTLDADD)
 load_file_event_LDADD	= $(TESTLDADD)
 lock_file_event_LDADD	= $(TESTLDADD)
 
+noinst_LIBRARIES	= libtest.a
+
+libtest_a_SOURCES	= timer.c util.c
+
 TESTS_ENVIRONMENT=top_srcdir=$(top_srcdir)
test/it-works.c +44 -24

114 lines not on this page · raw message

test/load-file-event.c +48 -60

182 lines not on this page · raw message

test/lock-file-event.c +68 -50

220 lines not on this page · raw message

test/save-file-event.c +48 -61

184 lines not on this page · raw message

test/test.h new file +26 -0
new file mode 100644
index 0000000..ec72b2b
@@ -0,0 +1,26 @@
+#ifndef _CLD_TEST_H_
+#define _CLD_TEST_H_
+
+#include <stdbool.h>
+#include <cldc.h>
+#include "timer.h"
+
+#define TESTSTR          "longertestdata\n"
+#define TESTLEN  (sizeof("longertestdata\n")-1)
+
+#define LOCKSTR          "testlock\n"
+#define LOCKLEN  (sizeof("testlock\n")-1)
+
+#define TFNAME     "/cld-test-inst"
+#define TLNAME     "/cld-lock-inst"
+
+#define TEST_HOST "localhost"
+
+#define TEST_USER "testuser"
+#define TEST_USER_KEY "testuser"
+
+#define TEST_PORTFILE_CLD	"cld.port"
+
+extern void test_loop(struct cldc_udp *udp, int timec, struct timer *timev[]);
+
+#endif
test/timer.c new file +44 -0

44 lines not on this page · raw message

test/timer.h new file +18 -0
new file mode 100644
index 0000000..e0ddcc5
@@ -0,0 +1,18 @@
+#ifndef _TEST_TIMER_H_
+#define _TEST_TIMER_H_
+
+#include <stdbool.h>
+#include <time.h>
+
+struct timer {
+	time_t expires;
+	void (*func)(void *);
+	void *priv;
+};
+
+extern void timer_init(struct timer *timer, void (*func)(void *), void *priv);
+extern void timer_add(struct timer *timer, time_t expires);
+extern void timer_del(struct timer *timer);
+extern bool timer_poke(struct timer *timer, time_t now, time_t *pset);
+
+#endif
test/util.c new file +66 -0

66 lines not on this page · raw message

tools/Makefile.am +1 -1
index c171a78..2a6e1a5 100644
@@ -5,5 +5,5 @@ INCLUDES	= -I$(top_srcdir)/include	\
 bin_PROGRAMS	= cldcli
 
 cldcli_LDADD	= ../lib/libcldc.la		\
-		  @GLIB_LIBS@ @CRYPTO_LIBS@ @EVENT_LIBS@ @ARGP_LIBS@
+		  @GLIB_LIBS@ @CRYPTO_LIBS@ @ARGP_LIBS@ @RESOLV_LIBS@
 
#1 lore raw
Quoted 64 lines · Pete Zaitcev
test/timer.c new file +44 -0
new file mode 100644
index 0000000..ce05196
@@ -0,0 +1,44 @@
+
+/*
+ * Basic timers for CLD test suite.
+ */
+#include <string.h>
+#include "timer.h"
+
+void timer_init(struct timer *timer, void (*func)(void *), void *priv)
+{
+	memset(timer, 0, sizeof(struct timer));
+	timer->func = func;
+	timer->priv = priv;
+}
+
+void timer_add(struct timer *timer, time_t expires)
+{
+	timer->expires = expires;
+}
+
+void timer_del(struct timer *timer)
+{
+	timer->expires = 0;
+}
+
+/*
+ * Return true if timer was run.
+ * Otherwise, modify *pset if our expected expiration time is closer.
+ */
+bool timer_poke(struct timer *timer, time_t now, time_t *pset)
+{
+	if (!timer->expires)
+		return false;
+
+	if (now >= timer->expires) {
+		timer->expires = 0;
+		(*timer->func)(timer->priv);
+		return true;
+	}
+
+	if (*pset == 0 || timer->expires < *pset)
+		*pset = timer->expires;
+	return false;
+}
+
test/timer.h new file +8 -0
new file mode 100644
index 0000000..e0ddcc5
@@ -0,0 +1,18 @@
+#ifndef _TEST_TIMER_H_
+#define _TEST_TIMER_H_
+
+#include <stdbool.h>
+#include <time.h>
+
+struct timer {
+	time_t expires;

test/timer.h line 18

Quoted 10 lines
test/timer.h new file +10 -0
+	void (*func)(void *);
+	void *priv;
+};
+
+extern void timer_init(struct timer *timer, void (*func)(void *), void *priv);
+extern void timer_add(struct timer *timer, time_t expires);
+extern void timer_del(struct timer *timer);
+extern bool timer_poke(struct timer *timer, time_t now, time_t *pset);
+
+#endif

hmmm, you are re-creating the self-contained timer facilities that already exist in CLD, which are designed to be used with any of select(2), poll(2), or epoll_wait(2). I don't think we need two hand-coded timer implementations.

I would suggest using some variant of the changes below as patch #1, then add these other changes on top of that. I have not committed these changes, pending your comments.

If you choose to take the changes below and submit them as part of your own patch series, make sure to add "From: Jeff Garzik <[email protected]>" as the first line of the email body. This so-called "Andrew Morton rule" triggers 'git am' and other utilities to differentiate patch author from email author.

 include/Makefile.am |    2 -
 include/libtimer.h  |   34 ++++++++++++++++++++
 lib/.gitignore      |    2 +
 lib/Makefile.am     |    2 +
 lib/libtimer.c      |   83 +++++++++++++++++++++++++++++++++++++++++++++++++
 server/Makefile.am  |    3 +
 server/cld.h        |   25 --------------
 server/util.c       |   87 ----------------------------------------------------
 8 files changed, 125 insertions(+), 113 deletions(-)
include/Makefile.am +1 -1
index 06e35a6..f7539dd 100644
@@ -1,5 +1,5 @@
 
-EXTRA_DIST = cld-private.h
+EXTRA_DIST = cld-private.h libtimer.h
 
 include_HEADERS = cldc.h cld_msg.h
 
include/libtimer.h new file +34 -0
new file mode 100644
index 0000000..aedaea0
@@ -0,0 +1,34 @@
+
+#ifndef __LIBTIMER_H__
+#define __LIBTIMER_H__
+
+#include <stdbool.h>
+#include <string.h>
+#include <time.h>
+
+struct timer {
+	bool			fired;
+	bool			on_list;
+	void			(*cb)(struct timer *);
+	void			*userdata;
+	time_t			expires;
+	char			name[32];
+};
+
+extern void timer_add(struct timer *timer, time_t expires);
+extern void timer_del(struct timer *timer);
+extern time_t timers_run(void);
+
+static inline void timer_init(struct timer *timer, const char *name,
+			      void (*cb)(struct timer *),
+			      void *userdata)
+{
+	memset(timer, 0, sizeof(*timer));
+	timer->cb = cb;
+	timer->userdata = userdata;
+	strncpy(timer->name, name, sizeof(timer->name));
+	timer->name[sizeof(timer->name) - 1] = 0;
+}
+
+#endif /* __LIBTIMER_H__ */
+
lib/.gitignore +2 -0
index 9eaa296..dddcc4c 100644
@@ -6,5 +6,7 @@ libcldc.la
 libcldc-uninstalled.pc
 libcldc.pc
 
+libtimer.a
+
 .libs
 
lib/Makefile.am +2 -0
index 5b042ef..68be429 100644
@@ -16,6 +16,8 @@ libcldc_la_LDFLAGS = \
 	-no-undefined \
 	-export-symbols-regex "^[^_].*"
 
+noinst_LIBRARIES	= libtimer.a
+
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libcldc.pc
 
lib/libtimer.c new file +83 -0

83 lines not on this page · raw message

server/Makefile.am +2 -1
index 929a3fd..d099b4a 100644
@@ -8,7 +8,8 @@ sbin_PROGRAMS	= cld cldbadm
 cld_SOURCES	= cldb.h cld.h \
 		  ../lib/common.c \
 		  cldb.c msg.c server.c session.c util.c
-cld_LDADD	= @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
+cld_LDADD	= ../lib/libtimer.a \
+		  @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
 
 cldbadm_SOURCES	= cldb.h cldbadm.c
 cldbadm_LDADD	= @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@
server/cld.h +1 -24
index da26862..05c93ad 100644
@@ -26,8 +26,8 @@
 #include <glib.h>
 #include "cldb.h"
 #include <cld_msg.h>
+#include <libtimer.h>
 
-struct timer;
 struct client;
 struct session_outpkt;
 
@@ -40,15 +40,6 @@ enum {
 	SFL_FOREGROUND		= (1 << 0),	/* run in foreground */
 };
 
-struct timer {
-	bool			fired;
-	bool			on_list;
-	void			(*cb)(struct timer *);
-	void			*userdata;
-	time_t			expires;
-	char			name[32];
-};
-
 struct client {
 	struct sockaddr_in6	addr;		/* inet address */
 	socklen_t		addr_len;	/* inet address len */
@@ -176,20 +167,6 @@ extern void applog(int prio, const char *fmt, ...);
 extern int write_pid_file(const char *pid_fn);
 extern void syslogerr(const char *prefix);
 extern int fsetflags(const char *prefix, int fd, int or_flags);
-extern void timer_add(struct timer *timer, time_t expires);
-extern void timer_del(struct timer *timer);
-extern time_t timers_run(void);
-
-static inline void timer_init(struct timer *timer, const char *name,
-			      void (*cb)(struct timer *),
-			      void *userdata)
-{
-	memset(timer, 0, sizeof(*timer));
-	timer->cb = cb;
-	timer->userdata = userdata;
-	strncpy(timer->name, name, sizeof(timer->name));
-	timer->name[sizeof(timer->name) - 1] = 0;
-}
 
 #ifndef HAVE_STRNLEN
 extern size_t strnlen(const char *s, size_t maxlen);
server/util.c +0 -87

99 lines not on this page · raw message

#2 lore raw
Quoted 3 lines · On Sat, 28 Nov 2009 04:07:32 -0500 Jeff Garzik <[email protected]> wrote:

I would suggest using some variant of the changes below as patch #1, then add these other changes on top of that. I have not committed these changes, pending your comments.

Thanks. I should've done it myself. Don't know what I was thinking, I knew that these routines existed.

-- Pete