1
1
mirror of https://github.com/jrbrtsn/ban2fail synced 2024-06-16 11:58:01 +00:00

Implemented ez_libpthread

This commit is contained in:
john 2019-12-05 11:00:39 -05:00
parent 37065c0539
commit db444512dc
7 changed files with 280 additions and 41 deletions

@ -20,6 +20,7 @@ src := \
ez_es.c \ ez_es.c \
ez_libc.c \ ez_libc.c \
ez_libdb.c \ ez_libdb.c \
ez_libpthread.c \
ez_libz.c \ ez_libz.c \
iptables.c \ iptables.c \
logType.c \ logType.c \

@ -92,7 +92,7 @@ struct Global G= {
.version= { .version= {
.major= 0, .major= 0,
.minor= 13, .minor= 13,
.patch= 8 .patch= 9
}, },
.bitTuples.flags= GlobalFlagBitTuples .bitTuples.flags= GlobalFlagBitTuples

43
es.c

@ -9,7 +9,8 @@
#include "util.h" #include "util.h"
#include "map.h" #include "map.h"
#include "msgqueue.h" #include "msgqueue.h"
#include "es.h" #include "ez_es.h"
#include "ez_libpthread.h"
/* Types of registered callbacks */ /* Types of registered callbacks */
enum ES_type { enum ES_type {
@ -318,7 +319,7 @@ initialize()
*/ */
{ {
/* Get the global mutex */ /* Get the global mutex */
if(pthread_mutex_lock(&S.mtx)) assert (0); ez_pthread_mutex_lock(&S.mtx);
/* Processwide static data */ /* Processwide static data */
if(!(S.flags & GLOBAL_INIT_FLG)) { if(!(S.flags & GLOBAL_INIT_FLG)) {
@ -328,7 +329,7 @@ initialize()
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 */
if(pthread_mutex_unlock(&S.mtx)) assert (0); ez_pthread_mutex_unlock(&S.mtx);
/* Per-thread static data */ /* Per-thread static data */
PTRVEC_constructor(&TS.fd_vec, 10); PTRVEC_constructor(&TS.fd_vec, 10);
@ -344,15 +345,15 @@ initialize()
TS.tid= pthread_self(); TS.tid= pthread_self();
/* Add ourself to the vsig thread to TS map */ /* Add ourself to the vsig thread to TS map */
if(pthread_mutex_lock(&S.vsig.mtx)) assert (0); ez_pthread_mutex_lock(&S.vsig.mtx);
MAP_addTypedKey(&S.vsig.thrd_ts_map, TS.tid, &TS); MAP_addTypedKey(&S.vsig.thrd_ts_map, TS.tid, &TS);
if(pthread_mutex_unlock(&S.vsig.mtx)) assert (0);
/*--- virtual signal infrastructure ---*/ /*--- virtual signal infrastructure ---*/
MSGQUEUE_constructor(&TS.vsig.mq, sizeof(int), VSIG_QUEUE_MAX); MSGQUEUE_constructor(&TS.vsig.mq, sizeof(int), VSIG_QUEUE_MAX);
MAP_constructor(&TS.vsig.map, 10, 10); MAP_constructor(&TS.vsig.map, 10, 10);
/* Register a signal handler for SIGUSR2 so we can have virtual signals. */ /* Register a signal handler for SIGUSR2 so we can have virtual signals. */
if(-1 == ES_registerSignal(SIGUSR2, sigusr2_h, NULL)) assert(0); ez_ES_registerSignal(SIGUSR2, sigusr2_h, NULL);
ez_pthread_mutex_unlock(&S.vsig.mtx);
} }
@ -843,7 +844,7 @@ ES_spawn_thread_sched(
} }
/* Get the global mutex */ /* Get the global mutex */
if(pthread_mutex_lock(&S.spawn.mtx)) assert (0); ez_pthread_mutex_lock(&S.spawn.mtx);
/* Get the condition ready for use */ /* Get the condition ready for use */
pthread_cond_init(&S.spawn.cond, NULL); pthread_cond_init(&S.spawn.cond, NULL);
@ -852,29 +853,25 @@ ES_spawn_thread_sched(
S.spawn.release_parent= 0; S.spawn.release_parent= 0;
/* Get the condition mutex */ /* Get the condition mutex */
if(pthread_mutex_lock(&S.spawn.cond_mtx)) assert (0); ez_pthread_mutex_lock(&S.spawn.cond_mtx);
/* Spawn the new thread */ /* Spawn the new thread */
memset(&tid, 0, sizeof(tid)); memset(&tid, 0, sizeof(tid));
/* JDR Sat 30 Nov 2019 10:39:04 AM EST /* JDR Sat 30 Nov 2019 10:39:04 AM EST
* it appears that this fails at 300 threads. * it appears that this fails at 300 threads.
*/ */
rtn = pthread_create (&tid, &attr, user_main, arg); ez_pthread_create (&tid, &attr, user_main, arg);
if(rtn) {
sys_eprintf("ERROR: pthread_create()");
abort();
}
/* Now we, the parent, wait on the child */ /* Now we, the parent, wait on the child */
while(!S.spawn.release_parent) { while(!S.spawn.release_parent) {
if(pthread_cond_wait(&S.spawn.cond, &S.spawn.cond_mtx)) assert(0); ez_pthread_cond_wait(&S.spawn.cond, &S.spawn.cond_mtx);
} }
/* Release the condition mutex */ /* Release the condition mutex */
if(pthread_mutex_unlock(&S.spawn.cond_mtx)) assert (0); ez_pthread_mutex_unlock(&S.spawn.cond_mtx);
/* Release the global lock */ /* Release the global lock */
if(pthread_mutex_unlock(&S.spawn.mtx)) assert (0); ez_pthread_mutex_unlock(&S.spawn.mtx);
return tid; return tid;
} }
@ -887,16 +884,16 @@ ES_release_parent(void)
*/ */
{ {
/* Condition manipulation must be protected by a mutex */ /* Condition manipulation must be protected by a mutex */
if (pthread_mutex_lock (&S.spawn.cond_mtx)) assert (0); ez_pthread_mutex_lock (&S.spawn.cond_mtx);
/* Note that parent may be released */ /* Note that parent may be released */
S.spawn.release_parent= 1; S.spawn.release_parent= 1;
/* Signal the parent */ /* Signal the parent */
if(pthread_cond_signal(&S.spawn.cond)) assert(0); ez_pthread_cond_signal(&S.spawn.cond);
/* Free up the condition mutex */ /* Free up the condition mutex */
if (pthread_mutex_unlock (&S.spawn.cond_mtx)) assert (0); ez_pthread_mutex_unlock (&S.spawn.cond_mtx);
} }
@ -909,9 +906,8 @@ ES_cleanup(void)
assert(TS.tid == pthread_self()); assert(TS.tid == pthread_self());
/* Remove ourself from the vsig thread to TS map */ /* Remove ourself from the vsig thread to TS map */
if(pthread_mutex_lock(&S.vsig.mtx)) assert (0); ez_pthread_mutex_lock(&S.vsig.mtx);
MAP_removeTypedItem(&S.vsig.thrd_ts_map, TS.tid); MAP_removeTypedItem(&S.vsig.thrd_ts_map, TS.tid);
if(pthread_mutex_unlock(&S.vsig.mtx)) assert (0);
{ /* Destroy key map */ { /* Destroy key map */
unsigned len= MAP_numItems(&TS.key_map); unsigned len= MAP_numItems(&TS.key_map);
@ -943,6 +939,7 @@ ES_cleanup(void)
for(unsigned i= 0; i < NUMSIGS; ++i) { for(unsigned i= 0; i < NUMSIGS; ++i) {
PTRVEC_destructor(TS.sig_vec_arr+i); PTRVEC_destructor(TS.sig_vec_arr+i);
} }
ez_pthread_mutex_unlock(&S.vsig.mtx);
} }
int int
@ -960,9 +957,8 @@ ES_VSignal (pthread_t tid, int signum)
{ {
int rtn= EOF-1; int rtn= EOF-1;
/* find the correct TS by thread identifier */ /* find the correct TS by thread identifier */
if(pthread_mutex_lock(&S.vsig.mtx)) assert (0); ez_pthread_mutex_lock(&S.vsig.mtx);
struct _TS *ts= MAP_findTypedItem(&S.vsig.thrd_ts_map, tid); struct _TS *ts= MAP_findTypedItem(&S.vsig.thrd_ts_map, tid);
if(pthread_mutex_unlock(&S.vsig.mtx)) assert (0);
if(!ts) { if(!ts) {
eprintf("ERROR: tid= %s not found!", pthread_t_str(tid)); eprintf("ERROR: tid= %s not found!", pthread_t_str(tid));
@ -982,5 +978,6 @@ ES_VSignal (pthread_t tid, int signum)
rtn= 0; rtn= 0;
abort: abort:
ez_pthread_mutex_unlock(&S.vsig.mtx);
return rtn; return rtn;
} }

151
ez_libpthread.c Normal file

@ -0,0 +1,151 @@
/***************************************************************************
* Copyright (C) 2019 by John D. Robertson *
* john@rrci.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#define _GNU_SOURCE
#include <errno.h>
#include <stdlib.h>
#include "ez_libpthread.h"
#include "util.h"
/***************************************************/
int _ez_pthread_mutex_lock(
const char *fileName,
int lineNo,
const char *funcName,
pthread_mutex_t *mutex
)
{
int rtn= pthread_mutex_lock (mutex);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_mutex_lock() failed");
abort();
}
/***************************************************/
int _ez_pthread_mutex_unlock(
const char *fileName,
int lineNo,
const char *funcName,
pthread_mutex_t *mutex
)
{
int rtn= pthread_mutex_unlock (mutex);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_mutex_unlock() failed");
abort();
}
/***************************************************/
int _ez_pthread_create(
const char *fileName,
int lineNo,
const char *funcName,
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg
)
{
int rtn= pthread_create (thread, attr, start_routine, arg);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_mutex_create() failed");
abort();
}
/***************************************************/
int _ez_pthread_cond_signal(
const char *fileName,
int lineNo,
const char *funcName,
pthread_cond_t *cond
)
{
int rtn= pthread_cond_signal (cond);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_cond_signal() failed");
abort();
}
/***************************************************/
int _ez_pthread_cond_wait(
const char *fileName,
int lineNo,
const char *funcName,
pthread_cond_t *cond,
pthread_mutex_t *mutex
)
{
int rtn= pthread_cond_wait (cond, mutex);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_cond_wait() failed");
abort();
}
/***************************************************/
int _ez_pthread_join(
const char *fileName,
int lineNo,
const char *funcName,
pthread_t thread,
void **retval
)
{
int rtn= pthread_join (thread, retval);
if(0 == rtn) return 0;
errno= rtn;
_sys_eprintf((const char*(*)(int))strerror
#ifdef DEBUG
, fileName, lineNo, funcName
#endif
, "pthread_join() failed");
abort();
}

94
ez_libpthread.h Normal file

@ -0,0 +1,94 @@
/***************************************************************************
* Copyright (C) 2019 by John D. Robertson *
* john@rrci.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef EZ_LIBPTHREAD_H
#define EZ_LIBPTHREAD_H
/* Simplified interface to libpthread functions */
#define _GNU_SOURCE
#include <pthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#define ez_pthread_mutex_lock(mutex) \
_ez_pthread_mutex_lock(__FILE__, __LINE__, __FUNCTION__, mutex)
int _ez_pthread_mutex_lock(
const char *fileName,
int lineNo,
const char *funcName,
pthread_mutex_t *mutex
);
#define ez_pthread_mutex_unlock(mutex) \
_ez_pthread_mutex_unlock(__FILE__, __LINE__, __FUNCTION__, mutex)
int _ez_pthread_mutex_unlock(
const char *fileName,
int lineNo,
const char *funcName,
pthread_mutex_t *mutex
);
#define ez_pthread_create(thread, addr, start_routine, arg) \
_ez_pthread_create(__FILE__, __LINE__, __FUNCTION__, thread, addr, start_routine, arg)
int _ez_pthread_create(
const char *fileName,
int lineNo,
const char *funcName,
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg
);
#define ez_pthread_cond_signal(cond) \
_ez_pthread_cond_signal(__FILE__, __LINE__, __FUNCTION__, cond)
int _ez_pthread_cond_signal(
const char *fileName,
int lineNo,
const char *funcName,
pthread_cond_t *cond
);
#define ez_pthread_cond_wait(cond, mutex) \
_ez_pthread_cond_wait(__FILE__, __LINE__, __FUNCTION__, cond, mutex)
int _ez_pthread_cond_wait(
const char *fileName,
int lineNo,
const char *funcName,
pthread_cond_t *cond,
pthread_mutex_t *mutex
);
#define ez_pthread_join(thread, retval) \
_ez_pthread_join(__FILE__, __LINE__, __FUNCTION__, thread, retval)
int _ez_pthread_join(
const char *fileName,
int lineNo,
const char *funcName,
pthread_t thread,
void **retval
);
#ifdef __cplusplus
}
#endif
#endif

@ -23,6 +23,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "ez_libpthread.h"
#include "msgqueue.h" #include "msgqueue.h"
#include "util.h" #include "util.h"
@ -66,8 +67,7 @@ MSGQUEUE_submitMsg (MSGQUEUE * self, const void *msgBuf)
{ {
int rtn = 0; int rtn = 0;
if (pthread_mutex_lock (&self->mtx)) ez_pthread_mutex_lock (&self->mtx);
assert (0);
if (self->numItems == self->maxItems) if (self->numItems == self->maxItems)
{ {
@ -87,8 +87,7 @@ MSGQUEUE_submitMsg (MSGQUEUE * self, const void *msgBuf)
++self->numItems; ++self->numItems;
abort: abort:
if (pthread_mutex_unlock (&self->mtx)) ez_pthread_mutex_unlock (&self->mtx);
assert (0);
return rtn; return rtn;
} }
@ -100,8 +99,7 @@ MSGQUEUE_extractMsg (MSGQUEUE * self, void *msgBuf)
{ {
int rtn = 0; int rtn = 0;
if (pthread_mutex_lock (&self->mtx)) ez_pthread_mutex_lock (&self->mtx);
assert (0);
if (!self->numItems) if (!self->numItems)
{ {
rtn = EOF; rtn = EOF;
@ -115,8 +113,7 @@ MSGQUEUE_extractMsg (MSGQUEUE * self, void *msgBuf)
self->head = (self->head + 1) % self->maxItems; self->head = (self->head + 1) % self->maxItems;
abort: abort:
if (pthread_mutex_unlock (&self->mtx)) ez_pthread_mutex_unlock (&self->mtx);
assert (0);
return rtn; return rtn;
} }
@ -135,8 +132,7 @@ MSGQUEUE_checkQueue (MSGQUEUE * self,
unsigned int i; unsigned int i;
void *ptr; void *ptr;
if (pthread_mutex_lock (&self->mtx)) ez_pthread_mutex_lock (&self->mtx);
assert (0);
if (!self->numItems) if (!self->numItems)
goto abort; goto abort;
@ -151,8 +147,7 @@ MSGQUEUE_checkQueue (MSGQUEUE * self,
} }
abort: abort:
if (pthread_mutex_unlock (&self->mtx)) ez_pthread_mutex_unlock (&self->mtx);
assert (0);
return rtn; return rtn;
} }

11
pdns.c

@ -25,6 +25,7 @@
#include "ez_es.h" #include "ez_es.h"
#include "ez_libc.h" #include "ez_libc.h"
#include "ez_libpthread.h"
#include "msgqueue.h" #include "msgqueue.h"
#include "pdns.h" #include "pdns.h"
#include "util.h" #include "util.h"
@ -261,7 +262,7 @@ join_f(void *data, int signo)
struct worker *wrk= S.workerArr + signo; struct worker *wrk= S.workerArr + signo;
void *pRtn; void *pRtn;
pthread_join(wrk->tid, &pRtn); ez_pthread_join(wrk->tid, &pRtn);
wrk->is_joined= 1; wrk->is_joined= 1;
@ -416,11 +417,11 @@ worker_check_inbox_f(void *vp_ndx, int signo)
#ifdef qqDEBUG #ifdef qqDEBUG
if(!strcmp(msg.e->addr, "113.183.137.246")) { if(!strcmp(msg.e->addr, "113.183.137.246")) {
pthread_mutex_lock(&S.prt_mtx); ez_pthread_mutex_lock(&S.prt_mtx);
ez_fprintf(stderr, "rc= %d, %s ----------------------------------\n", rc, msg.e->addr); ez_fprintf(stderr, "rc= %d, %s ----------------------------------\n", rc, msg.e->addr);
addrinfo_print(res, stderr); addrinfo_print(res, stderr);
fflush(stderr); fflush(stderr);
pthread_mutex_unlock(&S.prt_mtx); ez_pthread_mutex_unlock(&S.prt_mtx);
} }
#endif #endif
if(res) freeaddrinfo(res); if(res) freeaddrinfo(res);
@ -460,11 +461,11 @@ if(!strcmp(msg.e->addr, "113.183.137.246")) {
#ifdef qqDEBUG #ifdef qqDEBUG
if(!strcmp(msg.e->addr, "113.183.137.246")) { if(!strcmp(msg.e->addr, "113.183.137.246")) {
pthread_mutex_lock(&S.prt_mtx); ez_pthread_mutex_lock(&S.prt_mtx);
ez_fprintf(stderr, "rc= %d, %s (%s) ----------------------------------\n", rc, msg.e->addr, msg.e->dns.name); ez_fprintf(stderr, "rc= %d, %s (%s) ----------------------------------\n", rc, msg.e->addr, msg.e->dns.name);
addrinfo_print(res, stderr); addrinfo_print(res, stderr);
fflush(stderr); fflush(stderr);
pthread_mutex_unlock(&S.prt_mtx); ez_pthread_mutex_unlock(&S.prt_mtx);
} }
#endif #endif
switch(rc) { switch(rc) {