Skip to content

Commit 731efdd

Browse files
committedMar 8, 2009
wrangle evsql_test to work
1 parent 743d31c commit 731efdd

File tree

8 files changed

+216
-7
lines changed

8 files changed

+216
-7
lines changed
 

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(CMAKE_MODULE_PATH "${evsql_SOURCE_DIR}/cmake/Modules/")
99

1010
# dependancies
1111
find_package(LibEvent REQUIRED)
12+
find_package(LibPQ REQUIRED)
1213

1314
# add the src subdir
1415
add_subdirectory (src)

‎cmake/Modules/FindLibEvent.cmake

-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ include (LibFindMacros)
1212
# include dir
1313
find_path (LibEvent_INCLUDE_DIR
1414
NAMES "event2/event.h"
15-
PATHS "$ENV{LIBEVENT_PREFIX}/include"
1615
)
1716

1817
# library
1918
find_library (LibEvent_LIBRARY
2019
NAMES "event"
21-
PATHS "$ENV{LIBEVENT_PREFIX}/lib"
2220
)
2321

2422
# set the external vars

‎cmake/Modules/FindLibPQ.cmake

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Find libevent
2+
# Once done, this will define:
3+
#
4+
# LibPQ_FOUND
5+
# LibPQ_INCLUDE_DIRS
6+
# LibPQ_LIBRARIES
7+
#
8+
# Currently, this only supports libevent-svn (i.e. 1.5/2.0), so it's kind of useless for real use :)
9+
10+
include (LibFindMacros)
11+
12+
# include dir
13+
find_path (LibPQ_INCLUDE_DIR
14+
NAMES "postgresql/libpq-fe.h"
15+
PATHS "$ENV{POSTGRESQL_PREFIX}/include"
16+
)
17+
18+
# library
19+
find_library (LibPQ_LIBRARY
20+
NAMES "pq"
21+
PATHS "$ENV{POSTGRESQL_PREFIX}/lib"
22+
)
23+
24+
# set the external vars
25+
set (LibPQ_PROCESS_INCLUDES LibPQ_INCLUDE_DIR)
26+
set (LibPQ_PROCESS_LIBS LibPQ_LIBRARY)
27+
libfind_process (LibPQ)

‎src/CMakeLists.txt

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# add our include path
2-
include_directories (${evsql_SOURCE_DIR}/include ${LibEvent_INCLUDE_DIRS})
2+
include_directories (${LibEvent_INCLUDE_DIRS})
33

44
# define our source code modules
55
set (LIB_SOURCES "lib/log.c")
@@ -8,13 +8,17 @@ set (EVSQL_SOURCES core.c util.c)
88

99
# XXX: silly cmake does silly things when you SET with only one arg
1010
set (EVSQL_SOURCES lib/log.c evpq.c core.c query.c result.c util.c)
11+
set (EVSQL_LIBRARIES ${LibEvent_LIBRARIES} ${LibPQ_LIBRARIES})
12+
13+
set (CFLAGS "-Wall -Wextra")
1114

1215
# add our library
1316
add_library (evsql ${EVSQL_SOURCES})
1417

1518
# set target attributes
16-
target_link_libraries (evsql ${LibEvent_LIBRARIES})
19+
target_link_libraries (evsql ${EVSQL_LIBRARIES})
1720
set_target_properties (evsql PROPERTIES
21+
COMPILE_FLAGS ${CFLAGS}
1822
FRAMEWORK True
1923
PUBLIC_HEADER include/evsql.h
2024
)
@@ -25,3 +29,11 @@ install (TARGETS evsql
2529
ARCHIVE DESTINATION lib/static
2630
PUBLIC_HEADER DESTINATION include
2731
)
32+
33+
# test stuff
34+
add_executable (evsql_test EXCLUDE_FROM_ALL lib/log.c lib/signals.c evsql_test.c)
35+
target_link_libraries (evsql_test evsql)
36+
set_target_properties (evsql_test PROPERTIES
37+
COMPILE_FLAGS ${CFLAGS}
38+
)
39+

‎src/evsql_test.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ void query_start (struct event_base *base, struct evsql *db) {
3131
static struct event ev;
3232
struct timeval tv = { 5, 0 };
3333

34-
evperiodic_assign(&ev, base, &tv, &query_timer, db);
35-
event_add(&ev, &tv);
34+
// XXX: disabled as evperiod_assign has gone missing
35+
// evperiodic_assign(&ev, base, &tv, &query_timer, db);
36+
// event_add(&ev, &tv);
3637

3738
INFO("[evsql_test.timer_start] started timer");
3839
}
@@ -275,5 +276,7 @@ error :
275276
if (ev_base)
276277
event_base_free(ev_base);
277278

279+
// XXX: err
280+
return 0;
278281
}
279282

‎src/lib/log.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void _generic_err_exit (int flags, const char *func, int err, const char *fmt, .
2525
__attribute__ ((format (printf, 4, 5)))
2626
__attribute__ ((noreturn));
2727

28-
static inline void debug_dummy (int dummy, ...) { /* no-op */ }
28+
static inline void debug_dummy (int dummy, ...) { (void) dummy; /* no-op */ }
2929

3030
enum _debug_level {
3131
DEBUG_FATAL,

‎src/lib/signals.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#define _GNU_SOURCE
2+
#include <signal.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <assert.h>
6+
7+
#include "signals.h"
8+
#include "log.h"
9+
10+
struct signals {
11+
struct event_base *ev_base;
12+
13+
struct signal {
14+
struct event *ev;
15+
} sig_list[MAX_SIGNALS];
16+
17+
int sig_count;
18+
};
19+
20+
void signals_loopexit (int signal, short event, void *arg) {
21+
struct signals *signals = arg;
22+
23+
(void) event;
24+
25+
INFO("[signal] caught %s: exiting the event loop", strsignal(signal));
26+
27+
if (event_base_loopexit(signals->ev_base, NULL))
28+
FATAL("event_base_loopexit");
29+
}
30+
31+
void signals_ignore (int signal, short event, void *arg) {
32+
struct signals *signals = arg;
33+
34+
(void) signal;
35+
(void) event;
36+
(void) arg;
37+
(void) signals;
38+
39+
/* ignore */
40+
}
41+
42+
struct signals *signals_alloc (struct event_base *ev_base) {
43+
struct signals *signals = NULL;
44+
45+
if ((signals = calloc(1, sizeof(*signals))) == NULL)
46+
ERROR("calloc");
47+
48+
// simple attributes
49+
signals->ev_base = ev_base;
50+
51+
// done
52+
return signals;
53+
54+
error:
55+
return NULL;
56+
}
57+
58+
int signals_add (struct signals *signals, int sigval, void (*sig_handler)(evutil_socket_t, short, void *)) {
59+
struct signal *sig_info;
60+
61+
// find our sig_info
62+
assert(signals->sig_count < MAX_SIGNALS);
63+
sig_info = &signals->sig_list[signals->sig_count++];
64+
65+
// set up the libevent signal events
66+
if ((sig_info->ev = evsignal_new(signals->ev_base, sigval, sig_handler, signals)) == NULL)
67+
PERROR("signal_new");
68+
69+
if (evsignal_add(sig_info->ev, NULL))
70+
PERROR("signal_add");
71+
72+
// success
73+
return 0;
74+
75+
error:
76+
return -1;
77+
}
78+
79+
struct signals *signals_default (struct event_base *ev_base) {
80+
struct signals *signals = NULL;
81+
82+
// alloc signals
83+
if ((signals = signals_alloc(ev_base)) == NULL)
84+
return NULL;
85+
86+
// add the set of default signals
87+
if ( signals_add(signals, SIGPIPE, &signals_ignore)
88+
|| signals_add(signals, SIGINT, &signals_loopexit)
89+
) ERROR("signals_add");
90+
91+
// success
92+
return signals;
93+
94+
error:
95+
if (signals)
96+
signals_free(signals);
97+
98+
return NULL;
99+
}
100+
101+
void signals_free (struct signals *signals) {
102+
int i;
103+
104+
// free all events
105+
for (i = 0; i < signals->sig_count; i++) {
106+
if (evsignal_del(signals->sig_list[i].ev))
107+
PWARNING("signal_del");
108+
109+
}
110+
111+
// free the info itself
112+
free(signals);
113+
}
114+

‎src/lib/signals.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef LIB_SIGNAL_H
2+
#define LIB_SIGNAL_H
3+
4+
/*
5+
* Handle signals in a libevent-sane way
6+
*/
7+
8+
#include <event2/event.h>
9+
10+
/*
11+
* How many signals we can define actions for
12+
*/
13+
#define MAX_SIGNALS 8
14+
15+
/*
16+
* info about a set of signals
17+
*/
18+
struct signals;
19+
20+
/*
21+
* Used as a handler for signals that should cause a loopexit.
22+
*/
23+
void signals_loopexit (int signal, short event, void *arg);
24+
25+
/*
26+
* Used to receive signals, but discard them.
27+
*/
28+
void signals_ignore (int signal, short event, void *arg);
29+
30+
/*
31+
* Allocate a signals struct, acting on the given ev_base.
32+
*
33+
* Returns NULL on failure
34+
*/
35+
struct signals *signals_alloc (struct event_base *ev_base);
36+
37+
/*
38+
* Add a signal to be handled by the given signals struct with the given handler.
39+
*/
40+
int signals_add (struct signals *signals, int sigval, void (*sig_handler)(evutil_socket_t, short, void *));
41+
42+
/*
43+
* Add a set of default signals
44+
* SIGPIPE signals_ignore
45+
* SIGINT signals_loopexit
46+
*/
47+
struct signals *signals_default (struct event_base *ev_base);
48+
49+
/*
50+
* Free the resources/handlers associated with the given signal handler
51+
*/
52+
void signals_free (struct signals *signals);
53+
54+
#endif /* LIB_SIGNAL_H */

0 commit comments

Comments
 (0)