update ez files

This commit is contained in:
John D. Robertson 2020-09-15 11:31:27 -04:00
parent 9594faef04
commit 4ad2ea2af9
15 changed files with 1319 additions and 1716 deletions

277
es.c
View File

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -6,11 +7,11 @@
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
#include "util.h" #include "es.h"
#include "msgqueue.h"
#include "ez_es.h"
#include "map.h"
#include "ez_libpthread.h" #include "ez_libpthread.h"
#include "map.h"
#include "msgqueue.h"
#include "util.h"
/* Types of registered callbacks */ /* Types of registered callbacks */
enum ES_type { enum ES_type {
@ -79,9 +80,14 @@ static _Thread_local struct _TS {
*/ */
pthread_t tid; pthread_t tid;
enum {
TS_PROCESSING_FLG= 1<<0
} flags;
/* Vectors of Cb by type, for fast processing */ /* Vectors of Cb by type, for fast processing */
PTRVEC fd_vec, PTRVEC fd_vec,
timer_vec; timer_vec,
deleted_vec;
PTRVEC sig_vec_arr[NUMSIGS]; // One vector for each Unix signal PTRVEC sig_vec_arr[NUMSIGS]; // One vector for each Unix signal
/* Hash table to quickly find Cb's */ /* Hash table to quickly find Cb's */
@ -98,6 +104,7 @@ static _Thread_local struct _TS {
MAP cb_map; MAP cb_map;
} vsig; } vsig;
} TS; } TS;
static void static void
@ -115,6 +122,10 @@ UnixSignalHandler (int signo)
/******************************************************************/ /******************************************************************/
typedef struct { typedef struct {
enum {
CB_DELETED_FLG=1<<31
} flags;
int64_t lastActivity_ms; int64_t lastActivity_ms;
/* Process-wide unique integer */ /* Process-wide unique integer */
@ -188,6 +199,7 @@ Cb_FdConstructor(
*/ */
{ {
assert(self); assert(self);
memset(self, 0, sizeof(*self));
self->key= ++S.keySrc; self->key= ++S.keySrc;
self->type= ES_FD_TYPE; self->type= ES_FD_TYPE;
self->un.fd.fd= fd; self->un.fd.fd= fd;
@ -213,6 +225,7 @@ Cb_SignalConstructor(
*/ */
{ {
assert(self); assert(self);
memset(self, 0, sizeof(*self));
self->key= ++S.keySrc; self->key= ++S.keySrc;
self->type= ES_SIG_TYPE; self->type= ES_SIG_TYPE;
self->un.sig.signum= signum; self->un.sig.signum= signum;
@ -235,6 +248,7 @@ Cb_VSignalConstructor(
*/ */
{ {
assert(self); assert(self);
memset(self, 0, sizeof(*self));
self->key= ++S.keySrc; self->key= ++S.keySrc;
self->type= ES_VSIG_TYPE; self->type= ES_VSIG_TYPE;
self->un.sig.signum= signum; self->un.sig.signum= signum;
@ -258,6 +272,7 @@ Cb_TimerConstructor(
*/ */
{ {
assert(self); assert(self);
memset(self, 0, sizeof(*self));
self->key= ++S.keySrc; self->key= ++S.keySrc;
self->type= ES_TIMER_TYPE; self->type= ES_TIMER_TYPE;
#if ! (defined (_WIN32) || defined (__CYGWIN__)) #if ! (defined (_WIN32) || defined (__CYGWIN__))
@ -337,7 +352,8 @@ initialize()
if(!(S.flags & GLOBAL_INIT_FLG)) { if(!(S.flags & GLOBAL_INIT_FLG)) {
S.flags |= GLOBAL_INIT_FLG; S.flags |= GLOBAL_INIT_FLG;
if(-1 == sigemptyset(&S.dflt_sa.set)) assert(0); if(-1 == sigemptyset(&S.dflt_sa.set))
assert(0);
MAP_constructor(&S.vsig.thrd_ts_map, 10, 10); MAP_constructor(&S.vsig.thrd_ts_map, 10, 10);
} }
/* Release the global mutex */ /* Release the global mutex */
@ -346,10 +362,12 @@ initialize()
/* Per-thread static data */ /* Per-thread static data */
PTRVEC_constructor(&TS.fd_vec, 10); PTRVEC_constructor(&TS.fd_vec, 10);
PTRVEC_constructor(&TS.timer_vec, 10); PTRVEC_constructor(&TS.timer_vec, 10);
PTRVEC_constructor(&TS.deleted_vec, 10);
for(int i= 0; i < NUMSIGS; ++i) { for(int i= 0; i < NUMSIGS; ++i) {
PTRVEC_constructor(&TS.sig_vec_arr[i], 10); PTRVEC_constructor(&TS.sig_vec_arr[i], 10);
} }
if(-1 == sigemptyset(&TS.sigsRaised)) assert(0); if(-1 == sigemptyset(&TS.sigsRaised))
assert(0);
MAP_constructor(&TS.key_map, 10, 10); MAP_constructor(&TS.key_map, 10, 10);
@ -411,16 +429,19 @@ ES_registerSignal (
sigaddset(&S.dflt_sa.set, signum); sigaddset(&S.dflt_sa.set, signum);
if (sigaction (signum, &act, &S.dflt_sa.arr[ndx])) assert (0); if (sigaction (signum, &act, &S.dflt_sa.arr[ndx]))
assert (0);
} else { } else {
if (sigaction (signum, &act, NULL)) assert (0); if (sigaction (signum, &act, NULL))
assert (0);
} }
} }
if(!Cb_SignalCreate(cb, signum, callback_f, ctxt)) assert(0); if(!Cb_SignalCreate(cb, signum, callback_f, ctxt))
assert(0);
/* All callbacks are put in the key table */ /* All callbacks are put in the key table */
MAP_addTypedKey(&TS.key_map, cb->key, cb); MAP_addTypedKey(&TS.key_map, cb->key, cb);
@ -455,7 +476,8 @@ ES_registerVSignal (
Cb *cb; Cb *cb;
if(!Cb_VSignalCreate(cb, signum, callback_f, ctxt)) assert(0); if(!Cb_VSignalCreate(cb, signum, callback_f, ctxt))
assert(0);
/* Place in the virtual signal map indexed on signum */ /* Place in the virtual signal map indexed on signum */
MAP_addTypedKey(&TS.vsig.cb_map, cb->un.sig.signum, cb); MAP_addTypedKey(&TS.vsig.cb_map, cb->un.sig.signum, cb);
@ -481,7 +503,8 @@ ES_registerFd (
if(TS.tid != pthread_self()) initialize(); if(TS.tid != pthread_self()) initialize();
Cb *cb; Cb *cb;
if(!Cb_FdCreate(cb, fd, events, callback_f, ctxt)) assert(0); if(!Cb_FdCreate(cb, fd, events, callback_f, ctxt))
assert(0);
/* Index to vector for quick processing */ /* Index to vector for quick processing */
PTRVEC_addTail(&TS.fd_vec, cb); PTRVEC_addTail(&TS.fd_vec, cb);
@ -505,7 +528,8 @@ ES_registerTimer (
if(TS.tid != pthread_self()) initialize(); if(TS.tid != pthread_self()) initialize();
Cb *cb; Cb *cb;
if(!Cb_TimerCreate(cb, pause_ms, interval_ms, callback_f, ctxt)) assert(0); if(!Cb_TimerCreate(cb, pause_ms, interval_ms, callback_f, ctxt))
assert(0);
/* Add to the timer vector */ /* Add to the timer vector */
PTRVEC_addTail(&TS.timer_vec, cb); PTRVEC_addTail(&TS.timer_vec, cb);
@ -525,17 +549,33 @@ ES_unregister (int key)
if(TS.tid != pthread_self()) initialize(); if(TS.tid != pthread_self()) initialize();
Cb *cb = MAP_findTypedItem(&TS.key_map, key); Cb *cb = MAP_findTypedItem(&TS.key_map, key);
if(!cb) return -1; if(!cb) {
#ifdef qqDEBUG
eprintf("WARNING: could not find callback with key= %d", key);
#endif
return -1;
}
/* If the callback processing is currently active, do not delete it */
if(TS.flags & TS_PROCESSING_FLG) {
if(!(cb->flags & CB_DELETED_FLG)) {
cb->flags |= CB_DELETED_FLG;
PTRVEC_addTail(&TS.deleted_vec, cb);
}
return 0;
}
/* Remove from key table */ /* Remove from key table */
if(!MAP_removeTypedItem(&TS.key_map, key)) assert(0);; if(!MAP_removeTypedItem(&TS.key_map, key))
assert(0);
/* Different operations needed based on type */ /* Different operations needed based on type */
switch(cb->type) { switch(cb->type) {
case ES_FD_TYPE: case ES_FD_TYPE:
/* Remove from file descriptor vector */ /* Remove from file descriptor vector */
if(!PTRVEC_remove(&TS.fd_vec, cb)) assert(0); if(!PTRVEC_remove(&TS.fd_vec, cb))
assert(0);
break; break;
case ES_SIG_TYPE: case ES_SIG_TYPE:
@ -543,7 +583,8 @@ ES_unregister (int key)
unsigned ndx= signum2dflt_sa_ndx(cb->un.sig.signum); unsigned ndx= signum2dflt_sa_ndx(cb->un.sig.signum);
/* Remove from appropriate signals vector */ /* Remove from appropriate signals vector */
if(!PTRVEC_remove(&TS.sig_vec_arr[ndx], cb)) assert(0); if(!PTRVEC_remove(&TS.sig_vec_arr[ndx], cb))
assert(0);
/* If there are no more signals in this vector */ /* If there are no more signals in this vector */
if(!PTRVEC_numItems(&TS.sig_vec_arr[ndx])) { if(!PTRVEC_numItems(&TS.sig_vec_arr[ndx])) {
@ -551,17 +592,20 @@ ES_unregister (int key)
assert(sigismember(&S.dflt_sa.set, cb->un.sig.signum)); assert(sigismember(&S.dflt_sa.set, cb->un.sig.signum));
/* Restore default signal handling */ /* Restore default signal handling */
if (sigaction (cb->un.sig.signum, &S.dflt_sa.arr[ndx], NULL)) assert (0); if (sigaction (cb->un.sig.signum, &S.dflt_sa.arr[ndx], NULL))
assert (0);
} }
} break; } break;
case ES_VSIG_TYPE: case ES_VSIG_TYPE:
if(!MAP_removeSpecificTypedItem(&TS.vsig.cb_map, cb->un.sig.signum, cb)) assert(0); if(!MAP_removeSpecificTypedItem(&TS.vsig.cb_map, cb->un.sig.signum, cb))
assert(0);
break; break;
case ES_TIMER_TYPE: case ES_TIMER_TYPE:
/* Remove from timer vector */ /* Remove from timer vector */
if(!PTRVEC_remove(&TS.timer_vec, cb)) assert(0); if(!PTRVEC_remove(&TS.timer_vec, cb))
assert(0);
break; break;
default: default:
@ -612,22 +656,23 @@ ES_run (void)
* Whatever nonzero value one of the callbacks() returned. * Whatever nonzero value one of the callbacks() returned.
*/ */
{ {
int rtn= -1;
if(TS.tid != pthread_self()) if(TS.tid != pthread_self())
initialize(); initialize();
/* Loop forever */ /* Loop forever */
for(;;) { for(;;) {
TS.flags |= TS_PROCESSING_FLG;
int numFds= PTRVEC_numItems(&TS.fd_vec); int numFds= PTRVEC_numItems(&TS.fd_vec);
struct pollfd pollItemArr[numFds]; struct pollfd pollItemArr[numFds];
Cb *cbArr[numFds]; Cb *cbArr[numFds];
/* This sort provides fair queuing */ /* This sort provides fair queuing */
PTRVEC_sort(&TS.fd_vec, lastActivity_cmp); PTRVEC_sort(&TS.fd_vec, lastActivity_cmp);
/****** Load up the ZeroMQ pollItemArr *****/ /****** Load up the pollItemArr *****/
unsigned i; unsigned i;
Cb *cb; Cb *cb;
PTRVEC_loopFwd(&TS.fd_vec, i, cb) { PTRVEC_loopFwd(&TS.fd_vec, i, cb) {
@ -694,8 +739,8 @@ ES_run (void)
switch(errno) { switch(errno) {
case EFAULT: case EFAULT:
eprintf("\tpoll() failed"); eprintf("ERROR: poll() failed");
return -1; goto abort;
case EINTR: case EINTR:
/* Signal caused poll() to return, which is OK */ /* Signal caused poll() to return, which is OK */
@ -725,10 +770,14 @@ ES_run (void)
assert(ES_SIG_TYPE == cb->type); assert(ES_SIG_TYPE == cb->type);
/* Call the callback function */ if(cb->flags & CB_DELETED_FLG)
int error= (* cb->un.sig.callback_f)(cb->ctxt, signum); continue;
if(error) return error; /* Call the callback function */
rtn= (* cb->un.sig.callback_f)(cb->ctxt, signum);
if(rtn)
goto abort;
} }
} }
} }
@ -747,6 +796,9 @@ ES_run (void)
PTRVEC_loopFwd(&TS.timer_vec, i, cb) { PTRVEC_loopFwd(&TS.timer_vec, i, cb) {
if(cb->flags & CB_DELETED_FLG)
continue;
/* See how much time remains for this callback */ /* See how much time remains for this callback */
remaining_ms= msec2timeout(cb, time_ms); remaining_ms= msec2timeout(cb, time_ms);
@ -757,7 +809,7 @@ ES_run (void)
++cb->un.timer.count; ++cb->un.timer.count;
/* Call the callback function */ /* Call the callback function */
int error= (* cb->un.timer.callback_f)(cb->ctxt); rtn= (* cb->un.timer.callback_f)(cb->ctxt);
/* If this is a single-shot timer, get rid of it now */ /* If this is a single-shot timer, get rid of it now */
if(!cb->un.timer.interval_ms) { if(!cb->un.timer.interval_ms) {
@ -767,9 +819,11 @@ ES_run (void)
} }
/* If the callback returned non-zero, bail out */ /* If the callback returned non-zero, bail out */
if(error) return error; if(rtn)
goto abort;
} else break; /* time remaining will increase from here on out */ } else
break; /* time remaining will increase from here on out */
} }
} }
@ -783,31 +837,36 @@ ES_run (void)
Cb *cb= cbArr[i]; Cb *cb= cbArr[i];
if(cb->flags & CB_DELETED_FLG)
continue;
#if ! (defined (_WIN32) || defined (__CYGWIN__)) #if ! (defined (_WIN32) || defined (__CYGWIN__))
cb->lastActivity_ms= clock_gettime_ms(CLOCK_MONOTONIC_COARSE); cb->lastActivity_ms= clock_gettime_ms(CLOCK_MONOTONIC_COARSE);
#else #else
cb->lastActivity_ms= clock_gettime_ms(CLOCK_MONOTONIC); cb->lastActivity_ms= clock_gettime_ms(CLOCK_MONOTONIC);
#endif #endif
int error; assert(ES_FD_TYPE == cb->type);
switch(cb->type) {
case ES_FD_TYPE: rtn= (* cb->un.fd.callback_f)(cb->ctxt, item->fd, item->revents);
/* Call the callback function */
error= (* cb->un.fd.callback_f)(cb->ctxt, item->fd, item->revents);
break;
default:
assert(0);
}
/* If the callback returned non-zero, bail out */ /* If the callback returned non-zero, bail out */
if(error) return error; if(rtn)
goto abort;
}
{ /*--- Free any callbacks marked for deletion ---*/
Cb *cb;
TS.flags &= ~TS_PROCESSING_FLG;
while((cb= PTRVEC_remTail(&TS.deleted_vec))) {
if(ES_unregister(cb->key))
assert(0);
}
} }
} }
/* Shouldn't ever get to here */ abort:
assert(0); return rtn;
} }
pthread_t pthread_t
@ -979,3 +1038,135 @@ abort:
ez_pthread_mutex_unlock(&S.vsig.mtx); ez_pthread_mutex_unlock(&S.vsig.mtx);
return rtn; return rtn;
} }
/*=====================================================================================*/
/*===================== ez_xxx() ======================================================*/
/*=====================================================================================*/
/***************************************************/
ez_proto (int, ES_registerFd,
int fd,
short events,
int (*callback_f)(void *ctxt, int fd, short events),
void *ctxt
)
{
int rtn= ES_registerFd(fd, events, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerFd() failed.");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, ES_registerSignal,
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt
)
{
int rtn= ES_registerSignal(signum, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerSignal() failed.");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, ES_registerVSignal,
int signum,
int (*callback_f)(void *ctxt,int signo),
void *ctxt
)
{
int rtn= ES_registerVSignal(signum, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerVSignal() failed.");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, ES_VSignal,
pthread_t tid,
int signum)
{
int rtn= ES_VSignal(tid, signum);
if(rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_VSignal() returned %d.", rtn);
abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, ES_registerTimer,
int64_t pause_ms,
int64_t interval_ms,
int (*callback_f)(void *ctxt),
void *ctxt
)
{
int rtn= ES_registerTimer(pause_ms, interval_ms, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerTimer() failed.");
abort();
}
return rtn;
}
/***************************************************/
ez_proto ( int, ES_unregister, int key)
{
int rtn= ES_unregister(key);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_unregister() failed.");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, ES_run)
{
int rtn= ES_run();
if(rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_run() returned %d", rtn);
abort();
}
return rtn;
}

90
es.h
View File

@ -7,20 +7,23 @@
* timers. * timers.
****************************************************************************************/ ****************************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <pthread.h> #include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include "ez.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int ez_hdr_proto (int, ES_registerFd,
ES_registerFd (
int fd, int fd,
short events, short events,
int (*callback_f)(void *ctxt, int fd, short events), int (*callback_f)(void *ctxt, int fd, short events),
void *ctxt void *ctxt);
);
/********************************************************************** /**********************************************************************
* Register a function to be called when there is activity on the * Register a function to be called when there is activity on the
* file descriptor (which may be a file, socket, pipe, etc. under Unix). * file descriptor (which may be a file, socket, pipe, etc. under Unix).
@ -34,13 +37,18 @@ ES_registerFd (
* If successful, a positive integer which can be used to unregister the callback. * If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
#ifdef DEBUG
# define ez_ES_registerFd(...) \
_ez_ES_registerFd(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerFd(...) \
_ez_ES_registerFd(__VA_ARGS__)
#endif
int ez_hdr_proto (int, ES_registerSignal,
ES_registerSignal (
int signum, int signum,
int (*callback_f)(void *ctxt, int signo), int (*callback_f)(void *ctxt, int signo),
void *ctxt void *ctxt);
);
/********************************************************************** /**********************************************************************
* Register a function to be called when a particular Unix signal is * Register a function to be called when a particular Unix signal is
* raised. Note: callback_f() is not called from a Unix signal handler, * raised. Note: callback_f() is not called from a Unix signal handler,
@ -54,13 +62,18 @@ ES_registerSignal (
* If successful, a positive integer which can be used to unregister the callback. * If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
#ifdef DEBUG
# define ez_ES_registerSignal(...) \
_ez_ES_registerSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerSignal(...) \
_ez_ES_registerSignal(__VA_ARGS__)
#endif
int ez_hdr_proto (int, ES_registerVSignal,
ES_registerVSignal (
int signum, int signum,
int (*callback_f)(void *ctxt,int signo), int (*callback_f)(void *ctxt,int signo),
void *ctxt void *ctxt);
);
/********************************************************************** /**********************************************************************
* Register a function to be called when a particular virtual signal is * Register a function to be called when a particular virtual signal is
* raised. Virtual signals are implemented on top of the Unix signal, SIGUSR2. * raised. Virtual signals are implemented on top of the Unix signal, SIGUSR2.
@ -73,9 +86,17 @@ ES_registerVSignal (
* If successful, a positive integer which can be used to unregister the callback. * If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
#ifdef DEBUG
# define ez_ES_registerVSignal(...) \
_ez_ES_registerVSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerVSignal(...) \
_ez_ES_registerVSignal(__VA_ARGS__)
#endif
int ez_hdr_proto (int, ES_VSignal,
ES_VSignal (pthread_t tid, int signum); pthread_t tid,
int signum);
/********************************************************************** /**********************************************************************
* Send a virtual signal to tid, which is multiplexed on SIGUSR2. * Send a virtual signal to tid, which is multiplexed on SIGUSR2.
* *
@ -86,9 +107,15 @@ ES_VSignal (pthread_t tid, int signum);
* 0: successful * 0: successful
* -1: failures. * -1: failures.
*/ */
#ifdef DEBUG
# define ez_ES_VSignal(...) \
_ez_ES_VSignal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_VSignal(...) \
_ez_ES_VSignal(__VA_ARGS__)
#endif
int ez_hdr_proto (int, ES_registerTimer,
ES_registerTimer (
int64_t pause_ms, int64_t pause_ms,
int64_t interval_ms, int64_t interval_ms,
int (*callback_f)(void *ctxt), int (*callback_f)(void *ctxt),
@ -109,11 +136,16 @@ ES_registerTimer (
* If successful, a positive integer which can be used to unregister the callback. * If successful, a positive integer which can be used to unregister the callback.
* On failure, -1 is returned. * On failure, -1 is returned.
*/ */
#ifdef DEBUG
# define ez_ES_registerTimer(...) \
_ez_ES_registerTimer(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_registerTimer(...) \
_ez_ES_registerTimer(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_unregister,
int key);
int
ES_unregister (int key);
/********************************************************************** /**********************************************************************
* Unegister a previously registered callback. * Unegister a previously registered callback.
* *
@ -123,10 +155,15 @@ ES_unregister (int key);
* 0 for success. * 0 for success.
* -1 for failure (key not found) * -1 for failure (key not found)
*/ */
#ifdef DEBUG
# define ez_ES_unregister(...) \
_ez_ES_unregister(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_unregister(...) \
_ez_ES_unregister(__VA_ARGS__)
#endif
ez_hdr_proto (int, ES_run);
int
ES_run (void);
/********************************************************************** /**********************************************************************
* For this thread, use poll() to process socket activity until one * For this thread, use poll() to process socket activity until one
* of the registered callback_f() returns non-zero. * of the registered callback_f() returns non-zero.
@ -134,6 +171,13 @@ ES_run (void);
* RETURNS: * RETURNS:
* Whatever nonzero value callback_f() returned. * Whatever nonzero value callback_f() returned.
*/ */
#ifdef DEBUG
# define ez_ES_run(...) \
_ez_ES_run(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_ES_run(...) \
_ez_ES_run(__VA_ARGS__)
#endif
pthread_t pthread_t
ES_spawn_thread_sched( ES_spawn_thread_sched(

128
ez.h Normal file
View File

@ -0,0 +1,128 @@
/*****************************************************************************
* In an effort to make C programming both easier and more secure, I present
* the EZ method.
*
* Reasoning for this is simple; Many function calls can fail for a variety of
* reasons, some or even most of which are obscure. However, it is both
* insecure and counterproductive not to check for said failures. Unfortunately
* coding proper checks and/or recovery from failures will involve (often
* complex) logic constructs, and detracts from understanding the primary logic
* of your source code.
*
* Sometimes your app needs to recover, and so you must supply this code, but
* more often than not a "boilplate" approach is sufficient. The EZ method is a
* consistent convention to supply centralized and reusable "boilerplate" error
* handling on an _optional_ basis, both for existing library functions and
* your own.
*
* Modern programming languages address this problem by throwing "exceptions",
* which by default result in your program getting terminated. To avoid
* termination you must write code to "catch" such exceptions, but this code
* exists separated in source code from the function call, and tends to be both
* confusing to read and hideous to look at. If you thought "goto" statements
* were in poor taste, then this will make you vomit.
*
* Enter the EZ method; from a practical coding standpoint you merely prepend
* "ez_" to any function call for which you desire default error handling. When
* an error occurs a very helpful error message is printed (in DEBUG mode this
* includes the source code location from which the function was called), and
* your program is terminated. If your app needs to recover from such an error,
* simply erase the "ez_" prefix from the function call and supply the recovery
* code, where God intended it to be - right where you made the function call!
*
* In effect the EZ method provides the utility of throwing exceptions, but
* without the catch - Woohoo!
*****************************************************************************/
#ifndef EZ_H
#define EZ_H
/*****************************************************************************
* These macros ease the drudgery of writing prototypes for the _ez_XXX()
* wrappers of functions, and reduce the liklihood of bugs due to typing
* errors.
*
* The elipses is for your function's argument list.
*****************************************************************************/
#ifdef DEBUG
#define ez_proto(rtn, funcname, ...) \
rtn _ez_##funcname( \
const char *fileName, \
int lineNo, \
const char *funcName, \
##__VA_ARGS__ \
)
#define ez_hdr_proto(rtn, funcname, ...) \
ez_proto(rtn, funcname, ##__VA_ARGS__); \
rtn funcname(__VA_ARGS__)
#else
#define ez_proto(rtn, funcname, ...) \
rtn _ez_##funcname(__VA_ARGS__)
#define ez_hdr_proto(rtn, funcname, ...) \
ez_proto(rtn, funcname, ##__VA_ARGS__); \
rtn funcname(__VA_ARGS__)
#endif
/*****************************************************************************
* For example, if you have the following function prototype:
*
* int myfunc (struct foo *f, double bar)
*
* In the header file where the function prototype goes,
* you will need this:
*
* ez_hdr_proto (int, myfunc, struct foo *f, double bar);
*
* #ifdef DEBUG
* # define ez_myfunc(...) \
* _ez_myfunc(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
* #else
* # define ez_myfunc(...) \
* _ez_myfunc(__VA_ARGS__)
* #endif
*
* This will expand to the standard function prototype as listed above, as well
* as the _ez_myfunc() prototype correct for either DEBUG version or production
* version, depending on whether or not DEBUG is #define'd. Also, there is now
* a macro ez_myfunc() which will expand to the correct _ez_myfunc()
* function call.
*
* In the implmentation (*.c) file, you will need to supply code for
* _ez_myfunc(), which looks like so:
*
* ez_proto(int, myfunc, struct foo *f, double bar)
* {
* // implementation goes here
* }
*
* Notice that the prototype section is is the same as what you placed in the
* corresponding header file, minus "_hdr" in the ez_hdr_proto() macro.
*
* For the case where you are supplying only the ez_XXX() version of an
* existing function found in some library 'liba2z', you can place the
* following in your own header file, "ez_liba2z.h":
*
* ez_proto(struct rtnType*, liba2z_funcname, struct foo *f, double bar);
*
* Everything else works similar to the previous examples.
* Now you can use the EZ error handling like so:
*
* #include "ez_liba2z.h"
* int main(int argc, char **argv)
* {
* struct foo Foo;
* double bar;
* struct rtnType *rtn= ez_liba2z_funcname(&Foo, bar);
* }
*
*****************************************************************************/
#endif

171
ez_es.c
View File

@ -1,171 +0,0 @@
#include <stdlib.h>
#include "util.h"
#include "ez_es.h"
/***************************************************/
int _ez_ES_registerFd (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd,
short events,
int (*callback_f)(void *ctxt, int fd, short events),
void *ctxt
)
{
int rtn= ES_registerFd(fd, events, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerFd() failed.");
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_registerSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt
)
{
int rtn= ES_registerSignal(signum, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerSignal() failed.");
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_registerVSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt
)
{
int rtn= ES_registerVSignal(signum, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerVSignal() failed.");
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_VSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_t tid,
int signum
)
{
int rtn= ES_VSignal(tid, signum);
if(rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_VSignal() returned %d.", rtn);
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_registerTimer (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int64_t pause_ms,
int64_t interval_ms,
int (*callback_f)(void *ctxt),
void *ctxt
)
{
int rtn= ES_registerTimer(pause_ms, interval_ms, callback_f, ctxt);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_registerTimer() failed.");
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_unregister (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int key
)
{
int rtn= ES_unregister(key);
if(-1 == rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_unregister() failed.");
abort();
}
return rtn;
}
/***************************************************/
int _ez_ES_run (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName
#endif
)
{
int rtn= ES_run();
if(rtn) {
_eprintf(
#ifdef DEBUG
fileName, lineNo, funcName,
#endif
"ES_run() returned %d", rtn);
abort();
}
return rtn;
}

144
ez_es.h
View File

@ -1,144 +0,0 @@
/****************************************************************************************************************************
* NOTE: look in "es.h" for function documentation. The ez_XXX() macros here only wrap the ES_XXX() functions
* for the purpose of boilerplate error handling.
*/
#ifndef EZ_ES_H
#define EZ_ES_H
#include "es.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef DEBUG
# define ez_ES_registerFd(fd, events, callback_f, ctxt) \
_ez_ES_registerFd(__FILE__, __LINE__, __FUNCTION__, fd, events, callback_f, ctxt)
#else
# define ez_ES_registerFd(fd, events, callback_f, ctxt) \
_ez_ES_registerFd(fd, events, callback_f, ctxt)
#endif
int _ez_ES_registerFd (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd,
short events,
int (*callback_f)(void *ctxt, int fd, short events),
void *ctxt
);
#ifdef DEBUG
# define ez_ES_registerSignal(signum, callback_f, ctxt) \
_ez_ES_registerSignal(__FILE__, __LINE__, __FUNCTION__, signum, callback_f, ctxt)
#else
# define ez_ES_registerSignal(signum, callback_f, ctxt) \
_ez_ES_registerSignal(signum, callback_f, ctxt)
#endif
int _ez_ES_registerSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt
);
#ifdef DEBUG
# define ez_ES_registerVSignal(signum, callback_f, ctxt) \
_ez_ES_registerVSignal(__FILE__, __LINE__, __FUNCTION__, signum, callback_f, ctxt)
#else
# define ez_ES_registerVSignal(signum, callback_f, ctxt) \
_ez_ES_registerVSignal(signum, callback_f, ctxt)
#endif
int _ez_ES_registerVSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int signum,
int (*callback_f)(void *ctxt, int signo),
void *ctxt
);
#ifdef DEBUG
# define ez_ES_VSignal(tid, signum) \
_ez_ES_VSignal(__FILE__, __LINE__, __FUNCTION__, tid, signum)
#else
# define ez_ES_VSignal(tid, signum) \
_ez_ES_VSignal(tid, signum)
#endif
int _ez_ES_VSignal (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_t tid,
int signum
);
#ifdef DEBUG
# define ez_ES_registerTimer(pause_ms, interval_ms, callback_f, ctxt) \
_ez_ES_registerTimer(__FILE__, __LINE__, __FUNCTION__, pause_ms, interval_ms, callback_f, ctxt)
#else
# define ez_ES_registerTimer(pause_ms, interval_ms, callback_f, ctxt) \
_ez_ES_registerTimer(pause_ms, interval_ms, callback_f, ctxt)
#endif
int _ez_ES_registerTimer (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int64_t pause_ms,
int64_t interval_ms,
int (*callback_f)(void *ctxt),
void *ctxt
);
#ifdef DEBUG
# define ez_ES_unregister(key) \
{_ez_ES_unregister(__FILE__, __LINE__, __FUNCTION__, key); (key)= 0;}
#else
# define ez_ES_unregister(key) \
{_ez_ES_unregister(key); (key)= 0;}
#endif
int _ez_ES_unregister (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int key
);
#ifdef DEBUG
# define ez_ES_run() \
_ez_ES_run(__FILE__, __LINE__, __FUNCTION__)
#else
# define ez_ES_run() \
_ez_ES_run()
#endif
int _ez_ES_run (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName
#endif
);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -20,49 +20,40 @@
#define EZ_LIBANL_H #define EZ_LIBANL_H
/* Simplified interface to libanl functions */ /* Simplified interface to libanl functions */
#define _GNU_SOURCE #ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <netdb.h> #include <netdb.h>
#include "ez.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef DEBUG ez_proto(int, getaddrinfo_a,
# define ez_getaddrinfo_a(mode, list, nItems, sevp) \
_ez_getaddrinfo_a(__FILE__, __LINE__, __FUNCTION__, mode, list, nItems, sevp)
#else
# define ez_getaddrinfo_a(mode, list, nItems, sevp) \
_ez_getaddrinfo_a(mode, list, nItems, sevp)
#endif
int _ez_getaddrinfo_a(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int mode, int mode,
struct gaicb *list[], struct gaicb *list[],
int nitems, int nitems,
struct sigevent *sevp struct sigevent *sevp);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gai_suspend(list, nItems, timeout) \ # define ez_getaddrinfo_a(...) \
_ez_gai_suspend(__FILE__, __LINE__, __FUNCTION__, list, nItems, timeout) _ez_getaddrinfo_a(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gai_suspend(list, nItems, timeout) \ # define ez_getaddrinfo_a(...) \
_ez_gai_suspend(list, nItems, timeout) _ez_getaddrinfo_a(__VA_ARGS__)
#endif
int _ez_gai_suspend(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, gai_suspend,
const struct gaicb * const list[], const struct gaicb * const list[],
int nitems, int nitems,
const struct timespec *timeout const struct timespec *timeout);
); #ifdef DEBUG
# define ez_gai_suspend(...) \
_ez_gai_suspend(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_gai_suspend(...) \
_ez_gai_suspend(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus

507
ez_libc.c
View File

@ -25,15 +25,9 @@
#include "ez_libc.h" #include "ez_libc.h"
/***************************************************/ /***************************************************/
int _ez_fputs ( ez_proto (int, fputs,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *s, const char *s,
FILE *stream FILE *stream)
)
{ {
int rtn= fputs (s, stream); int rtn= fputs (s, stream);
if (EOF == rtn) { if (EOF == rtn) {
@ -48,15 +42,9 @@ int _ez_fputs (
} }
/***************************************************/ /***************************************************/
int _ez_fputc ( ez_proto (int, fputc,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int c, int c,
FILE *stream FILE *stream)
)
{ {
int rtn= fputc (c, stream); int rtn= fputc (c, stream);
if (EOF == rtn) { if (EOF == rtn) {
@ -71,16 +59,10 @@ int _ez_fputc (
} }
/***************************************************/ /***************************************************/
int _ez_fprintf ( ez_proto (int, fprintf,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
FILE *stream, FILE *stream,
const char *fmt, const char *fmt,
... ...)
)
{ {
va_list args; va_list args;
@ -94,7 +76,7 @@ int _ez_fprintf (
#ifdef DEBUG #ifdef DEBUG
, fileName, lineNo, funcName , fileName, lineNo, funcName
#endif #endif
, "vfprintf() failed"); , "vfprintf() failed returning %d", rtn);
abort(); abort();
} }
@ -102,15 +84,9 @@ int _ez_fprintf (
} }
/***************************************************/ /***************************************************/
FILE* _ez_popen ( ez_proto (FILE*, popen,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *command, const char *command,
const char *type const char *type)
)
{ {
errno= 0; errno= 0;
FILE *rtn= popen (command, type); FILE *rtn= popen (command, type);
@ -126,15 +102,27 @@ FILE* _ez_popen (
} }
/***************************************************/ /***************************************************/
FILE* _ez_fopen ( ez_proto (FILE*, fdopen,
int fd,
const char *mode)
{
FILE *rtn= fdopen (fd, mode);
if (!rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG #ifdef DEBUG
const char *fileName, , fileName, lineNo, funcName
int lineNo,
const char *funcName,
#endif #endif
, "fdopen(%d, \"%s\") failed", fd, mode);
abort();
}
return rtn;
}
/***************************************************/
ez_proto (FILE*, fopen,
const char *pathname, const char *pathname,
const char *mode const char *mode)
)
{ {
FILE *rtn= fopen (pathname, mode); FILE *rtn= fopen (pathname, mode);
if (!rtn) { if (!rtn) {
@ -149,14 +137,23 @@ FILE* _ez_fopen (
} }
/***************************************************/ /***************************************************/
int _ez_fclose ( ez_proto (pid_t, fork)
{
int rtn= fork ();
if (-1 == rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG #ifdef DEBUG
const char *fileName, , fileName, lineNo, funcName
int lineNo,
const char *funcName,
#endif #endif
FILE *stream , "fork() failed");
) abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, fclose,
FILE *stream)
{ {
int rtn= fclose (stream); int rtn= fclose (stream);
if (EOF == rtn) { if (EOF == rtn) {
@ -171,14 +168,8 @@ int _ez_fclose (
} }
/***************************************************/ /***************************************************/
int _ez_fflush ( ez_proto (int, fflush,
#ifdef DEBUG FILE *stream)
const char *fileName,
int lineNo,
const char *funcName,
#endif
FILE *stream
)
{ {
int rtn= fflush (stream); int rtn= fflush (stream);
if (EOF == rtn) { if (EOF == rtn) {
@ -193,17 +184,11 @@ int _ez_fflush (
} }
/***************************************************/ /***************************************************/
size_t _ez_fread ( ez_proto (size_t, fread,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
void *ptr, void *ptr,
size_t size, size_t size,
size_t nmemb, size_t nmemb,
FILE *stream FILE *stream)
)
{ {
size_t rtn= fread (ptr, size, nmemb, stream); size_t rtn= fread (ptr, size, nmemb, stream);
if (ferror(stream)) { if (ferror(stream)) {
@ -218,17 +203,11 @@ size_t _ez_fread (
} }
/***************************************************/ /***************************************************/
size_t _ez_fwrite ( ez_proto (size_t, fwrite,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const void *ptr, const void *ptr,
size_t size, size_t size,
size_t nmemb, size_t nmemb,
FILE *stream FILE *stream)
)
{ {
size_t rtn= fwrite (ptr, size, nmemb, stream); size_t rtn= fwrite (ptr, size, nmemb, stream);
if (ferror(stream)) { if (ferror(stream)) {
@ -243,14 +222,8 @@ size_t _ez_fwrite (
} }
/***************************************************/ /***************************************************/
int _ez_pclose ( ez_proto (int, pclose,
#ifdef DEBUG FILE *stream)
const char *fileName,
int lineNo,
const char *funcName,
#endif
FILE *stream
)
{ {
int rtn= pclose (stream); int rtn= pclose (stream);
if (-1 == rtn) { if (-1 == rtn) {
@ -265,16 +238,25 @@ int _ez_pclose (
} }
/***************************************************/ /***************************************************/
char* _ez_fgets ( ez_proto (int, pipe, int pipefd[2])
{
int rtn= pipe (pipefd);
if (-1 == rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG #ifdef DEBUG
const char *fileName, , fileName, lineNo, funcName
int lineNo,
const char *funcName,
#endif #endif
, "pipe() failed");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (char*, fgets,
char *s, char *s,
int size, int size,
FILE *stream FILE *stream)
)
{ {
char *rtn= fgets (s, size, stream); char *rtn= fgets (s, size, stream);
if (!rtn && !feof(stream)) { if (!rtn && !feof(stream)) {
@ -289,14 +271,8 @@ char* _ez_fgets (
} }
/***************************************************/ /***************************************************/
int _ez_remove ( ez_proto (int, remove,
#ifdef DEBUG const char *pathname)
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname
)
{ {
int rtn= remove (pathname); int rtn= remove (pathname);
if (-1 == rtn) { if (-1 == rtn) {
@ -311,15 +287,9 @@ int _ez_remove (
} }
/***************************************************/ /***************************************************/
int _ez_rename ( ez_proto (int, rename,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *oldpath, const char *oldpath,
const char *newpath const char *newpath)
)
{ {
int rtn= rename (oldpath, newpath); int rtn= rename (oldpath, newpath);
if (-1 == rtn) { if (-1 == rtn) {
@ -334,14 +304,8 @@ int _ez_rename (
} }
/***************************************************/ /***************************************************/
DIR* _ez_opendir ( ez_proto (DIR*, opendir,
#ifdef DEBUG const char *name)
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *name
)
{ {
DIR *rtn= opendir (name); DIR *rtn= opendir (name);
if (!rtn) { if (!rtn) {
@ -356,14 +320,8 @@ DIR* _ez_opendir (
} }
/***************************************************/ /***************************************************/
int _ez_closedir ( ez_proto (int, closedir,
#ifdef DEBUG DIR *dirp)
const char *fileName,
int lineNo,
const char *funcName,
#endif
DIR *dirp
)
{ {
int rtn= closedir (dirp); int rtn= closedir (dirp);
if (-1 == rtn) { if (-1 == rtn) {
@ -378,14 +336,40 @@ int _ez_closedir (
} }
/***************************************************/ /***************************************************/
struct dirent* _ez_readdir ( ez_proto (int, dup2, int oldfd, int newfd)
{
int rtn= dup2 (oldfd, newfd);
if (-1 == rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG #ifdef DEBUG
const char *fileName, , fileName, lineNo, funcName
int lineNo,
const char *funcName,
#endif #endif
DIR *dirp , "dup2() failed");
) abort();
}
return rtn;
}
/***************************************************/
ez_proto (int, execve,
const char *filename,
char *const argv[],
char *const envp[])
{
int rtn= execve(filename, argv, envp);
if (-1 == rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "execve() failed");
abort();
}
return rtn;
}
/***************************************************/
ez_proto (struct dirent*, readdir, DIR *dirp)
{ {
/* Pass on doctored format string and varargs to vfprintf() */ /* Pass on doctored format string and varargs to vfprintf() */
@ -405,14 +389,7 @@ struct dirent* _ez_readdir (
} }
/***************************************************/ /***************************************************/
int _ez_close ( ez_proto (int, close, int fd)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd
)
{ {
int rtn= close (fd); int rtn= close (fd);
if (-1 == rtn) { if (-1 == rtn) {
@ -427,16 +404,10 @@ int _ez_close (
} }
/***************************************************/ /***************************************************/
ssize_t _ez_write ( ez_proto (ssize_t, write,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd, int fd,
const void *buf, const void *buf,
size_t count size_t count)
)
{ {
ssize_t rtn= write (fd, buf, count); ssize_t rtn= write (fd, buf, count);
if (-1 == rtn) { if (-1 == rtn) {
@ -451,15 +422,9 @@ ssize_t _ez_write (
} }
/***************************************************/ /***************************************************/
int _ez_stat ( ez_proto (int, stat,
#ifdef DEBUG const char *pathname,
const char *fileName, struct stat *statbuf)
int lineNo,
const char *funcName,
#endif
const char *pathname,
struct stat *statbuf
)
{ {
int rtn= stat (pathname, statbuf); int rtn= stat (pathname, statbuf);
if (-1 == rtn) { if (-1 == rtn) {
@ -474,15 +439,9 @@ int _ez_stat (
} }
/***************************************************/ /***************************************************/
int _ez_mkdir ( ez_proto (int, mkdir,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname, const char *pathname,
mode_t mode mode_t mode)
)
{ {
int rtn= mkdir (pathname, mode); int rtn= mkdir (pathname, mode);
if (-1 == rtn) { if (-1 == rtn) {
@ -497,14 +456,8 @@ int _ez_mkdir (
} }
/***************************************************/ /***************************************************/
int _ez_rmdir ( ez_proto (int, rmdir,
#ifdef DEBUG const char *pathname)
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname
)
{ {
int rtn= rmdir (pathname); int rtn= rmdir (pathname);
if (-1 == rtn) { if (-1 == rtn) {
@ -519,14 +472,8 @@ int _ez_rmdir (
} }
/***************************************************/ /***************************************************/
int _ez_unlink ( ez_proto (int, unlink,
#ifdef DEBUG const char *pathname)
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname
)
{ {
int rtn= unlink (pathname); int rtn= unlink (pathname);
if (-1 == rtn) { if (-1 == rtn) {
@ -541,17 +488,11 @@ int _ez_unlink (
} }
/***************************************************/ /***************************************************/
int _ez_getaddrinfo( ez_proto (int, getaddrinfo,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *node, const char *node,
const char *service, const char *service,
const struct addrinfo *hints, const struct addrinfo *hints,
struct addrinfo **res struct addrinfo **res)
)
{ {
errno= 0; errno= 0;
int rtn= getaddrinfo (node, service, hints, res); int rtn= getaddrinfo (node, service, hints, res);
@ -583,20 +524,14 @@ int _ez_getaddrinfo(
} }
/***************************************************/ /***************************************************/
int _ez_getnameinfo( ez_proto (int, getnameinfo,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const struct sockaddr *addr, const struct sockaddr *addr,
socklen_t addrlen, socklen_t addrlen,
char *host, char *host,
socklen_t hostlen, socklen_t hostlen,
char *serv, char *serv,
socklen_t servlen, socklen_t servlen,
int flags int flags)
)
{ {
errno= 0; errno= 0;
int rtn= getnameinfo (addr, addrlen, host, hostlen, serv, servlen, flags); int rtn= getnameinfo (addr, addrlen, host, hostlen, serv, servlen, flags);
@ -622,20 +557,14 @@ int _ez_getnameinfo(
#ifdef DEBUG #ifdef DEBUG
, fileName, lineNo, funcName , fileName, lineNo, funcName
#endif #endif
, "getnameinfo() failed", rtn); , "getnameinfo() failed");
abort(); abort();
} }
/***************************************************/ /***************************************************/
int _ez_flock ( ez_proto (int, flock,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd, int fd,
int operation int operation)
)
{ {
errno= 0; errno= 0;
int rtn= flock (fd, operation); int rtn= flock (fd, operation);
@ -658,16 +587,10 @@ int _ez_flock (
} }
/***************************************************/ /***************************************************/
int _ez_open( ez_proto (int, open,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname, const char *pathname,
int flags, int flags,
mode_t mode mode_t mode)
)
{ {
errno= 0; errno= 0;
int rtn= open (pathname, flags, mode); int rtn= open (pathname, flags, mode);
@ -690,15 +613,9 @@ int _ez_open(
} }
/***************************************************/ /***************************************************/
int _ez_access( ez_proto (int, access,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname, const char *pathname,
int mode int mode)
)
{ {
errno= 0; errno= 0;
int rtn= access (pathname, mode); int rtn= access (pathname, mode);
@ -720,16 +637,10 @@ int _ez_access(
} }
/***************************************************/ /***************************************************/
char *_ez_strptime( ez_proto (char*, strptime,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *s, const char *s,
const char *format, const char *format,
struct tm *tm struct tm *tm)
)
{ {
char *rtn= strptime (s, format, tm); char *rtn= strptime (s, format, tm);
if(rtn) return rtn; if(rtn) return rtn;
@ -744,14 +655,7 @@ char *_ez_strptime(
} }
/***************************************************/ /***************************************************/
int _ez_seteuid( ez_proto (int, seteuid, uid_t euid)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
uid_t euid
)
{ {
int rtn= seteuid (euid); int rtn= seteuid (euid);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -765,14 +669,7 @@ int _ez_seteuid(
} }
/***************************************************/ /***************************************************/
int _ez_setegid( ez_proto (int, setegid, gid_t egid)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gid_t egid
)
{ {
int rtn= setegid (egid); int rtn= setegid (egid);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -786,14 +683,7 @@ int _ez_setegid(
} }
/***************************************************/ /***************************************************/
struct group* _ez_getgrnam( ez_proto (struct group*, getgrnam, const char *name)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *name
)
{ {
errno= 0; errno= 0;
struct group *rtn= getgrnam (name); struct group *rtn= getgrnam (name);
@ -821,16 +711,10 @@ struct group* _ez_getgrnam(
} }
/***************************************************/ /***************************************************/
int _ez_chown( ez_proto (int, chown,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *pathname, const char *pathname,
uid_t owner, uid_t owner,
gid_t group gid_t group)
)
{ {
int rtn= chown (pathname, owner, group); int rtn= chown (pathname, owner, group);
if(0 == rtn) return rtn; if(0 == rtn) return rtn;
@ -844,16 +728,10 @@ int _ez_chown(
} }
/***************************************************/ /***************************************************/
int _ez_fchown( ez_proto (int, fchown,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd, int fd,
uid_t owner, uid_t owner,
gid_t group gid_t group)
)
{ {
int rtn= fchown (fd, owner, group); int rtn= fchown (fd, owner, group);
if(0 == rtn) return rtn; if(0 == rtn) return rtn;
@ -867,15 +745,7 @@ int _ez_fchown(
} }
/***************************************************/ /***************************************************/
int _ez_fchmod( ez_proto (int, fchmod, int fd, mode_t mode)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
int fd,
mode_t mode
)
{ {
int rtn= fchmod (fd, mode); int rtn= fchmod (fd, mode);
if(0 == rtn) return rtn; if(0 == rtn) return rtn;
@ -889,14 +759,7 @@ int _ez_fchmod(
} }
/***************************************************/ /***************************************************/
int _ez_setuid( ez_proto (int, setuid, uid_t uid)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
uid_t uid
)
{ {
int rtn= setuid (uid); int rtn= setuid (uid);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -910,14 +773,7 @@ int _ez_setuid(
} }
/***************************************************/ /***************************************************/
int _ez_setgid( ez_proto (int, setgid, gid_t gid)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gid_t gid
)
{ {
int rtn= setgid (gid); int rtn= setgid (gid);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -929,3 +785,86 @@ int _ez_setgid(
, "setgid(%d) failed", (int)gid); , "setgid(%d) failed", (int)gid);
abort(); abort();
} }
/***************************************************/
ez_proto (int, atexit,
void(*function)(void))
{
int rtn= atexit (function);
if(0 == rtn) return 0;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "atexit() failed");
abort();
}
/***************************************************/
ez_proto (int, chdir,
const char *path)
{
int rtn= chdir (path);
if(0 == rtn) return 0;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "chdir(\"%s\") failed", path);
abort();
}
/***************************************************/
ez_proto (int, mkstemp,
char *template)
{
int rtn= mkstemp (template);
if(-1 != rtn) return rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "mkstemp(\"%s\") failed", template);
abort();
}
/***************************************************/
ez_proto (int, mkstemps,
char *template,
int suffixlen)
{
int rtn= mkstemps (template, suffixlen);
if(-1 != rtn) return rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "mkstemps(\"%s\") failed", template);
abort();
}
/***************************************************/
ez_proto (int, vfprintf,
FILE *stream,
const char *fmt,
va_list ap)
{
va_list arglist;
va_copy(arglist, ap);
int rtn= vfprintf (stream, fmt, arglist);
if (0 > rtn) {
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "vfprintf() failed");
abort();
}
return rtn;
}

1015
ez_libc.h

File diff suppressed because it is too large Load Diff

View File

@ -23,16 +23,10 @@
#include "ez_libdb.h" #include "ez_libdb.h"
/***************************************************/ /***************************************************/
int _ez_db_create( ez_proto (int, create,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB **dbp, DB **dbp,
DB_ENV *dbenv, DB_ENV *dbenv,
u_int32_t flags u_int32_t flags)
)
{ {
int rtn= db_create (dbp, dbenv, flags); int rtn= db_create (dbp, dbenv, flags);
@ -49,20 +43,14 @@ int _ez_db_create(
} }
/***************************************************/ /***************************************************/
int _ez_db_open( ez_proto (int, db_open,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
const char *file, const char *file,
const char *database, const char *database,
DBTYPE type, DBTYPE type,
u_int32_t flags, u_int32_t flags,
int mode int mode)
)
{ {
int rtn= db->open(db, txnid, file, database, type, flags, mode); int rtn= db->open(db, txnid, file, database, type, flags, mode);
@ -79,18 +67,12 @@ int _ez_db_open(
} }
/***************************************************/ /***************************************************/
int _ez_db_put( ez_proto (int, db_put,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
DBT *data, DBT *data,
u_int32_t flags u_int32_t flags)
)
{ {
int rtn= db->put(db, txnid, key, data, flags); int rtn= db->put(db, txnid, key, data, flags);
@ -107,18 +89,12 @@ int _ez_db_put(
} }
/***************************************************/ /***************************************************/
int _ez_db_get( ez_proto (int, db_get,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
DBT *data, DBT *data,
u_int32_t flags u_int32_t flags)
)
{ {
int rtn= db->get(db, txnid, key, data, flags); int rtn= db->get(db, txnid, key, data, flags);
@ -139,17 +115,11 @@ int _ez_db_get(
} }
/***************************************************/ /***************************************************/
int _ez_db_del( ez_proto (int, db_del,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
u_int32_t flags u_int32_t flags)
)
{ {
int rtn= db->del(db, txnid, key, flags); int rtn= db->del(db, txnid, key, flags);
@ -166,15 +136,7 @@ int _ez_db_del(
} }
/***************************************************/ /***************************************************/
int _ez_db_close( ez_proto (int, db_close, DB *db, u_int32_t flags)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db,
u_int32_t flags
)
{ {
int rtn= db->close(db, flags); int rtn= db->close(db, flags);
@ -191,15 +153,7 @@ int _ez_db_close(
} }
/***************************************************/ /***************************************************/
int _ez_db_fd( ez_proto (int, db_fd, DB *db, int *fdp)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db,
int *fdp
)
{ {
int rtn= db->fd(db, fdp); int rtn= db->fd(db, fdp);

View File

@ -20,145 +20,107 @@
#define EZ_LIBDB_H #define EZ_LIBDB_H
/* Simplified interface to libanl functions */ /* Simplified interface to libanl functions */
#define _GNU_SOURCE #ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <db.h> #include <db.h>
#include "ez.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef DEBUG ez_proto (int, create,
# define ez_db_create(dbp, dbenv, flags) \
_ez_db_create(__FILE__, __LINE__, __FUNCTION__, dbp, dbenv, flags)
#else
# define ez_db_create(dbp, dbenv, flags) \
_ez_db_create(dbp, dbenv, flags)
#endif
int _ez_db_create(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB **dbp, DB **dbp,
DB_ENV *dbenv, DB_ENV *dbenv,
u_int32_t flags u_int32_t flags);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_db_open(db, txnid, file, database, type, flags, mode ) \ # define ez_db_create(...) \
_ez_db_open(__FILE__, __LINE__, __FUNCTION__, db, txnid, file, database, type, flags, mode) _ez_db_create(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_db_open(db, txnid, file, database, type, flags, mode ) \ # define ez_db_create(...) \
_ez_db_open(db, txnid, file, database, type, flags, mode) _ez_db_create(__VA_ARGS__)
#endif
int _ez_db_open(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, db_open,
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
const char *file, const char *file,
const char *database, const char *database,
DBTYPE type, DBTYPE type,
u_int32_t flags, u_int32_t flags,
int mode int mode);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_db_put(db, txnid, key, data, flags) \ # define ez_db_open(...) \
_ez_db_put(__FILE__, __LINE__, __FUNCTION__, db, txnid, key, data, flags) _ez_db_open(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_db_put(db, txnid, key, data, flags) \ # define ez_db_open(...) \
_ez_db_put(db, txnid, key, data, flags) _ez_db_open(__VA_ARGS__)
#endif
int _ez_db_put(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, db_put,
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
DBT *data, DBT *data,
u_int32_t flags u_int32_t flags);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_db_get(db, txnid, key, data, flags) \ # define ez_db_put(...) \
_ez_db_get(__FILE__, __LINE__, __FUNCTION__, db, txnid, key, data, flags) _ez_db_put(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_db_get(db, txnid, key, data, flags) \ # define ez_db_put(...) \
_ez_db_get(db, txnid, key, data, flags) _ez_db_put(__VA_ARGS__)
#endif
int _ez_db_get(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, db_get,
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
DBT *data, DBT *data,
u_int32_t flags u_int32_t flags);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_db_del(db, txnid, key, flags) \ # define ez_db_get(...) \
_ez_db_del(__FILE__, __LINE__, __FUNCTION__, db, txnid, key, flags) _ez_db_get(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_db_del(db, txnid, key, flags) \ # define ez_db_get(...) \
_ez_db_del(db, txnid, key, flags) _ez_db_get(__VA_ARGS__)
#endif
int _ez_db_del(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, db_del,
DB *db, DB *db,
DB_TXN *txnid, DB_TXN *txnid,
DBT *key, DBT *key,
u_int32_t flags u_int32_t flags);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_db_close(db, flags) \ # define ez_db_del(...) \
_ez_db_close(__FILE__, __LINE__, __FUNCTION__, db, flags) _ez_db_del(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_db_close(db, flags) \ # define ez_db_del(...) \
_ez_db_close(db, flags) _ez_db_del(__VA_ARGS__)
#endif #endif
int _ez_db_close(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db,
u_int32_t flags
);
#ifdef DEBUG ez_proto (int, db_close,
# define ez_db_fd(db, fdp) \
_ez_db_fd(__FILE__, __LINE__, __FUNCTION__, db, fdp)
#else
# define ez_db_fd(db, fdp) \
_ez_db_fd(db, fdp)
#endif
int _ez_db_fd(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
DB *db, DB *db,
int *fdp u_int32_t flags);
); #ifdef DEBUG
# define ez_db_close(...) \
_ez_db_close(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_db_close(...) \
_ez_db_close(__VA_ARGS__)
#endif
ez_proto (int, db_fd,
DB *db,
int *fdp);
#ifdef DEBUG
# define ez_db_fd(...) \
_ez_db_fd(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_db_fd(...) \
_ez_db_fd(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -25,14 +25,7 @@
#include "util.h" #include "util.h"
/***************************************************/ /***************************************************/
int _ez_pthread_mutex_lock( ez_proto (int, pthread_mutex_lock, pthread_mutex_t *mutex)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_mutex_t *mutex
)
{ {
int rtn= pthread_mutex_lock (mutex); int rtn= pthread_mutex_lock (mutex);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -47,14 +40,7 @@ int _ez_pthread_mutex_lock(
} }
/***************************************************/ /***************************************************/
int _ez_pthread_mutex_unlock( ez_proto (int, pthread_mutex_unlock, pthread_mutex_t *mutex)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_mutex_t *mutex
)
{ {
int rtn= pthread_mutex_unlock (mutex); int rtn= pthread_mutex_unlock (mutex);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -69,17 +55,11 @@ int _ez_pthread_mutex_unlock(
} }
/***************************************************/ /***************************************************/
int _ez_pthread_create( ez_proto (int, pthread_create,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_t *thread, pthread_t *thread,
const pthread_attr_t *attr, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *(*start_routine) (void *),
void *arg void *arg)
)
{ {
int rtn= pthread_create (thread, attr, start_routine, arg); int rtn= pthread_create (thread, attr, start_routine, arg);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -94,14 +74,7 @@ int _ez_pthread_create(
} }
/***************************************************/ /***************************************************/
int _ez_pthread_cond_signal( ez_proto (int, pthread_cond_signal, pthread_cond_t *cond)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_cond_t *cond
)
{ {
int rtn= pthread_cond_signal (cond); int rtn= pthread_cond_signal (cond);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -116,15 +89,9 @@ int _ez_pthread_cond_signal(
} }
/***************************************************/ /***************************************************/
int _ez_pthread_cond_wait( ez_proto (int, pthread_cond_wait,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_cond_t *cond, pthread_cond_t *cond,
pthread_mutex_t *mutex pthread_mutex_t *mutex)
)
{ {
int rtn= pthread_cond_wait (cond, mutex); int rtn= pthread_cond_wait (cond, mutex);
if(0 == rtn) return 0; if(0 == rtn) return 0;
@ -139,15 +106,9 @@ int _ez_pthread_cond_wait(
} }
/***************************************************/ /***************************************************/
int _ez_pthread_join( ez_proto (int, pthread_join,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_t thread, pthread_t thread,
void **retval void **retval)
)
{ {
int rtn= pthread_join (thread, retval); int rtn= pthread_join (thread, retval);
if(0 == rtn) return 0; if(0 == rtn) return 0;

View File

@ -20,113 +20,81 @@
#define EZ_LIBPTHREAD_H #define EZ_LIBPTHREAD_H
/* Simplified interface to libpthread functions */ /* Simplified interface to libpthread functions */
#define _GNU_SOURCE #ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <pthread.h> #include <pthread.h>
#include "ez.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
ez_proto (int, pthread_mutex_lock,
pthread_mutex_t *mutex);
#ifdef DEBUG #ifdef DEBUG
# define ez_pthread_mutex_lock(mutex) \ # define ez_pthread_mutex_lock(...) \
_ez_pthread_mutex_lock(__FILE__, __LINE__, __FUNCTION__, mutex) _ez_pthread_mutex_lock(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_pthread_mutex_lock(mutex) \ # define ez_pthread_mutex_lock(...) \
_ez_pthread_mutex_lock(mutex) _ez_pthread_mutex_lock(__VA_ARGS__)
#endif #endif
int _ez_pthread_mutex_lock(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_mutex_t *mutex
);
ez_proto (int, pthread_mutex_unlock,
pthread_mutex_t *mutex);
#ifdef DEBUG #ifdef DEBUG
# define ez_pthread_mutex_unlock(mutex) \ # define ez_pthread_mutex_unlock(...) \
_ez_pthread_mutex_unlock(__FILE__, __LINE__, __FUNCTION__, mutex) _ez_pthread_mutex_unlock(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_pthread_mutex_unlock(mutex) \ # define ez_pthread_mutex_unlock(...) \
_ez_pthread_mutex_unlock(mutex) _ez_pthread_mutex_unlock(__VA_ARGS__)
#endif #endif
int _ez_pthread_mutex_unlock(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_mutex_t *mutex
);
#ifdef DEBUG ez_proto (int, pthread_create,
# define ez_pthread_create(thread, addr, start_routine, arg) \
_ez_pthread_create(__FILE__, __LINE__, __FUNCTION__, thread, addr, start_routine, arg)
#else
# define ez_pthread_create(thread, addr, start_routine, arg) \
_ez_pthread_create(thread, addr, start_routine, arg)
#endif
int _ez_pthread_create(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_t *thread, pthread_t *thread,
const pthread_attr_t *attr, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *(*start_routine) (void *),
void *arg void *arg);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_pthread_cond_signal(cond) \ # define ez_pthread_create(...) \
_ez_pthread_cond_signal(__FILE__, __LINE__, __FUNCTION__, cond) _ez_pthread_create(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_pthread_cond_signal(cond) \ # define ez_pthread_create(...) \
_ez_pthread_cond_signal(cond) _ez_pthread_create(__VA_ARGS__)
#endif #endif
int _ez_pthread_cond_signal(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
pthread_cond_t *cond
);
ez_proto (int, pthread_cond_signal,
pthread_cond_t *cond);
#ifdef DEBUG #ifdef DEBUG
# define ez_pthread_cond_wait(cond, mutex) \ # define ez_pthread_cond_signal(...) \
_ez_pthread_cond_wait(__FILE__, __LINE__, __FUNCTION__, cond, mutex) _ez_pthread_cond_signal(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_pthread_cond_wait(cond, mutex) \ # define ez_pthread_cond_signal(...) \
_ez_pthread_cond_wait(cond, mutex) _ez_pthread_cond_signal(__VA_ARGS__)
#endif
int _ez_pthread_cond_wait(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, pthread_cond_wait,
pthread_cond_t *cond, pthread_cond_t *cond,
pthread_mutex_t *mutex pthread_mutex_t *mutex);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_pthread_join(thread, retval) \ # define ez_pthread_cond_wait(...) \
_ez_pthread_join(__FILE__, __LINE__, __FUNCTION__, thread, retval) _ez_pthread_cond_wait(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_pthread_join(thread, retval) \ # define ez_pthread_cond_wait(...) \
_ez_pthread_join(thread, retval) _ez_pthread_cond_wait(__VA_ARGS__)
#endif
int _ez_pthread_join(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, pthread_join,
pthread_t thread, pthread_t thread,
void **retval void **retval);
); #ifdef DEBUG
# define ez_pthread_join(...) \
_ez_pthread_join(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_pthread_join(...) \
_ez_pthread_join(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -23,15 +23,9 @@
#include "util.h" #include "util.h"
/***************************************************/ /***************************************************/
gzFile _ez_gzopen( ez_proto (gzFile, gzopen,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *path, const char *path,
const char *mode const char *mode)
)
{ {
gzFile rtn= gzopen(path, mode); gzFile rtn= gzopen(path, mode);
if(rtn) return rtn; if(rtn) return rtn;
@ -45,14 +39,7 @@ gzFile _ez_gzopen(
} }
/***************************************************/ /***************************************************/
int _ez_gzclose( ez_proto (int, gzclose, gzFile file)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file
)
{ {
int err= gzclose(file); int err= gzclose(file);
if(Z_OK == err) return Z_OK; if(Z_OK == err) return Z_OK;
@ -91,16 +78,10 @@ int _ez_gzclose(
} }
/***************************************************/ /***************************************************/
int _ez_gzwrite( ez_proto (int, gzwrite,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file, gzFile file,
voidpc buf, voidpc buf,
unsigned len unsigned len)
)
{ {
int n= gzwrite(file, buf, len); int n= gzwrite(file, buf, len);
if(n == len) return n; if(n == len) return n;
@ -124,16 +105,10 @@ int _ez_gzwrite(
} }
/***************************************************/ /***************************************************/
int _ez_gzread( ez_proto (int, gzread,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file, gzFile file,
voidp buf, voidp buf,
unsigned len unsigned len)
)
{ {
int n= gzread(file, buf, len); int n= gzread(file, buf, len);
if(-1 != n) return n; if(-1 != n) return n;
@ -157,15 +132,7 @@ int _ez_gzread(
} }
/***************************************************/ /***************************************************/
int _ez_gzflush( ez_proto (int, gzflush, gzFile file, int flush)
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file,
int flush
)
{ {
int err= gzflush(file, flush); int err= gzflush(file, flush);
if(Z_OK == err) return Z_OK; if(Z_OK == err) return Z_OK;
@ -188,16 +155,10 @@ int _ez_gzflush(
} }
/***************************************************/ /***************************************************/
z_off_t _ez_gzseek( ez_proto (z_off_t, gzseek,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file, gzFile file,
z_off_t offset, z_off_t offset,
int whence int whence)
)
{ {
z_off_t rtn= gzseek(file, offset, whence); z_off_t rtn= gzseek(file, offset, whence);
if(-1 != rtn) return rtn; if(-1 != rtn) return rtn;
@ -221,16 +182,10 @@ z_off_t _ez_gzseek(
} }
/***************************************************/ /***************************************************/
char* _ez_gzgets ( ez_proto (char*, gzgets,
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file, gzFile file,
char *buf, char *buf,
int len int len)
)
{ {
char *rtn= gzgets(file, buf, len); char *rtn= gzgets(file, buf, len);
@ -259,14 +214,8 @@ char* _ez_gzgets (
} }
/***************************************************/ /***************************************************/
z_off_t _ez_gztell( ez_proto (z_off_t, _ez_gztell,
#ifdef DEBUG gzFile file)
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file
)
{ {
z_off_t rtn= gztell(file); z_off_t rtn= gztell(file);
if(-1 != rtn) return rtn; if(-1 != rtn) return rtn;

166
ez_libz.h
View File

@ -20,150 +20,106 @@
#define EZ_GZFILE_H #define EZ_GZFILE_H
/* Simplified interface to libz file functions */ /* Simplified interface to libz file functions */
#define _GNU_SOURCE #ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <zlib.h> #include <zlib.h>
#include "ez.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef DEBUG ez_proto (gzFile, gzopen,
# define ez_gzopen(path, mode) \
_ez_gzopen(__FILE__, __LINE__, __FUNCTION__, path, mode)
#else
# define ez_gzopen(path, mode) \
_ez_gzopen(path, mode)
#endif
gzFile _ez_gzopen(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
const char *path, const char *path,
const char *mode const char *mode);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzclose(file) \ # define ez_gzopen(...) \
_ez_gzclose(__FILE__, __LINE__, __FUNCTION__, file) _ez_gzopen(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzclose(file) \ # define ez_gzopen(...) \
_ez_gzclose(file) _ez_gzopen(__VA_ARGS__)
#endif #endif
int _ez_gzclose(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file
);
ez_proto (int, gzclose,
gzFile file);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzwrite(file, buf, len) \ # define ez_gzclose(...) \
_ez_gzwrite(__FILE__, __LINE__, __FUNCTION__, file, buf, len) _ez_gzclose(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzwrite(file, buf, len) \ # define ez_gzclose(...) \
_ez_gzwrite(file, buf, len) _ez_gzclose(__VA_ARGS__)
#endif
int _ez_gzwrite(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, gzwrite,
gzFile file, gzFile file,
voidpc buf, voidpc buf,
unsigned len unsigned len);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzread(file, buf, len) \ # define ez_gzwrite(...) \
_ez_gzread(__FILE__, __LINE__, __FUNCTION__, file, buf, len) _ez_gzwrite(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzread(file, buf, len) \ # define ez_gzwrite(...) \
_ez_gzread(file, buf, len) _ez_gzwrite(__VA_ARGS__)
#endif
int _ez_gzread(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, gzread,
gzFile file, gzFile file,
voidp buf, voidp buf,
unsigned len unsigned len);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzflush(file, flush) \ # define ez_gzread(...) \
_ez_gzflush(__FILE__, __LINE__, __FUNCTION__, file, flush) _ez_gzread(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzflush(file, flush) \ # define ez_gzread(...) \
_ez_gzflush(file, flush) _ez_gzread(__VA_ARGS__)
#endif
int _ez_gzflush(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (int, gzflush,
gzFile file, gzFile file,
int flush int flush);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzseek(file, offset, whence) \ # define ez_gzflush(...) \
_ez_gzseek(__FILE__, __LINE__, __FUNCTION__, file, offset, whence) _ez_gzflush(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzseek(file, offset, whence) \ # define ez_gzflush(...) \
_ez_gzseek(file, offset, whence) _ez_gzflush(__VA_ARGS__)
#endif
z_off_t _ez_gzseek(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (z_off_t, gzseek,
gzFile file, gzFile file,
z_off_t offset, z_off_t offset,
int whence int whence);
);
#ifdef DEBUG #ifdef DEBUG
# define ez_gztell(file) \ # define ez_gzseek(...) \
_ez_gztell(__FILE__, __LINE__, __FUNCTION__, file) _ez_gzseek(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gztell(file) \ # define ez_gzseek(...) \
_ez_gztell(file) _ez_gzseek(__VA_ARGS__)
#endif #endif
z_off_t _ez_gztell(
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif
gzFile file
);
ez_proto (z_off_t, _ez_gztell,
gzFile file);
#ifdef DEBUG #ifdef DEBUG
# define ez_gzgets(file, buf, len) \ # define ez_gztell(...) \
_ez_gzgets(__FILE__, __LINE__, __FUNCTION__, file, buf, len) _ez_gztell(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else #else
# define ez_gzgets(file, buf, len) \ # define ez_gztell(...) \
_ez_gzgets(file, buf, len) _ez_gztell(__VA_ARGS__)
#endif
char* _ez_gzgets (
#ifdef DEBUG
const char *fileName,
int lineNo,
const char *funcName,
#endif #endif
ez_proto (char*, gzgets,
gzFile file, gzFile file,
char *buf, char *buf,
int len int len);
); #ifdef DEBUG
# define ez_gzgets(...) \
_ez_gzgets(__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
# define ez_gzgets(...) \
_ez_gzgets(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

2
pdns.c
View File

@ -23,7 +23,7 @@
#include <limits.h> #include <limits.h>
#include <signal.h> #include <signal.h>
#include "ez_es.h" #include "es.h"
#include "ez_libc.h" #include "ez_libc.h"
#include "ez_libpthread.h" #include "ez_libpthread.h"
#include "msgqueue.h" #include "msgqueue.h"