#include <setjmp.h>
#include <sys/socket.h>
#include <stddef.h>
+#ifdef HAVE_PTHREAD
#include <pthread.h>
+#endif
-#include "common.h"
-#include "dataserv.h"
+#include "vblib.h"
const int safesigs[]={SIGQUIT,SIGINT,SIGTERM,SIGIOT,SIGALRM,-1}; //SIGCHLD->childsignal anyway
char vbnexact;
-const char *upsc_sshpath="/usr/bin/ssh"; /* upsetupconn()... */
-const char *upsc_dstport=DS_PORT;
void chk(const void *p)
{
vbsanity();
if (p) return;
- FATAL(CRIT,"NULL-check failure, memory exhausted? FATAL: %m");
+ VB_FATAL(CRIT,"NULL-check failure, memory exhausted? FATAL: %m");
}
#ifndef NDEBUG
#else
#define vbcheckno (0)
#endif
-pthread_mutex_t vbcheckmx=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#ifdef HAVE_PTHREAD
+static pthread_mutex_t vbcheckmx=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static void vbchecklock (void)
-{ if (pthread_mutex_lock (&vbcheckmx)) FATAL(CRIT,"Mutex ""lock on vbcheck mx"); }
+{ if (pthread_mutex_lock (&vbcheckmx)) VB_FATAL(CRIT,"Mutex ""lock on vbcheck mx"); }
static void vbcheckunlock(void)
-{ if (pthread_mutex_unlock(&vbcheckmx)) FATAL(CRIT,"Mutex unlock on vbcheck mx"); }
+{ if (pthread_mutex_unlock(&vbcheckmx)) VB_FATAL(CRIT,"Mutex unlock on vbcheck mx"); }
+#else
+#define vbchecklock()
+#define vbcheckunlock()
+#endif
static char vbchecklist(struct varbuf *vb)
{
char *d;
for (d=s;(d=memchr(d,'\0',s+len-d));) { *d++=' '; tot++; }
- if (tot) logmsg((quiet_null?MYLOG_DEBUG:MYLOG_ERR),
- "%s() replaced %u '\\0's by ' ' in: %.*s",fname,tot,(int)len,s);
+ if (!tot) return;
+#define ARG "%s() replaced %u '\\0's by ' ' in: %.*s",fname,tot,(int)len,s
+#define VB_LOG1(a,b) VB_LOG(a,b)
+ if (quiet_null)
+ VB_LOG1(DEBUG,ARG);
+ else
+ VB_LOG1(ERR,ARG);
+#undef VB_LOG1
+#undef ARG
}
char *vbgetline(struct varbuf *vb,char term)
{
size_t len=strlen(s);
if (len>PSTR_MAX) {
- LOG(CRIT,"String longer (%u) than PSTRing max (%u)",len,PSTR_MAX);
+ VB_LOG(CRIT,"String longer (%u) than PSTRing max (%u)",len,PSTR_MAX);
len=PSTR_MAX;
dbg("vbpstrputstring-longer than MAX: \"%s\"\n",s);
}
else if ((size=vbsize(vbs,len))<len) len=size;
was=len;
if (len>PSTR_MAX) {
- LOG(CRIT,"String longer (%u) than PSTRing max (%u)",len,PSTR_MAX);
+ VB_LOG(CRIT,"String longer (%u) than PSTRing max (%u)",len,PSTR_MAX);
len=PSTR_MAX;
vbdebug(vbs,"vbpstrputstring-longer than MAX");
}
i=vsnprintf(buf,sizeof(buf),fmt,ap);
va_end(ap);
if (i==-1) {
- LOG(CRIT,"Buffer overflow (max=%u) during PSTR printf",PSTR_MAX);
+ VB_LOG(CRIT,"Buffer overflow (max=%u) during PSTR printf",PSTR_MAX);
i=PSTR_MAX;
}
assert(i>=0 && i<=PSTR_MAX && !buf[i]);
chgd++;
}
if (chgd)
- LOG(CRIT,"'\\0's found (%u total) in read PSTR \"%s\", changed to '?'",chgd,buf);
+ VB_LOG(CRIT,"'\\0's found (%u total) in read PSTR \"%s\", changed to '?'",chgd,buf);
return(buf);
}
if (!fgets(buf,sizeof(buf),f)) {
dbg("vbgetlinef: fgets=NULL\n");
if (feof(f)) return;
- FATAL(ERR,"linereading fail: %m");
+ VB_FATAL(ERR,"linereading fail: %m");
}
dbg("vbgetlinef: fgets: %s\n",buf);
if ((s=strchr(buf,'\n'))) break;
static const struct chat_cmdtab chat_cmdint[]={
{"help" ,0,chatfn_help ,"This help"},
+#if 0
{"quit" ,0,chatfn_quit ,"Close connection"},
{"exit" ,0,chatfn_quit ,"Close connection"},
+#endif
};
static void chatfn_help(void)
vbprintf(chat_vbout,"205 %d parameter%s found, %d expected!\n",chat_cmdpar,(chat_cmdpar==1?"":"s"),cmd->npars);
else {
vbprintf(chat_vbout,"001 Running command: %s\n",cmds);
- LOG(DEBUG,"%s is executing user command \"%s\"",who,cmds);
+ VB_LOG(DEBUG,"%s is executing user command \"%s\"",who,cmds);
(*cmd->fnc)();
}
}
* Configuration section follows
*/
+/*
+ * PSTR_16BIT: whether PSTR strings should be preceded
+ * by 16-bit or 8-bit length (thus also limiting the maximum
+ * allowed length of PSTR string)
+ */
//#define PSTR_16BIT
+
+/*
+ * INLINE_PUTGETCHAR: inline vb{put,get}char()
+ */
#define INLINE_PUTGETCHAR
+
+/*
+ * FATALSIG_INSANE: if defined, no vbcheck()s are done after some fatal
+ * signal has been caught as the data structures may be corrupted
+ * already thereafter
+ */
//#define FATALSIG_INSANE
+/*
+ * HAVE_PTHREAD: vbcheck() serialization between threds
+ * Please note that VB library in general is NOT multithreading safe.
+ */
+//#define HAVE_PTHREAD
+
+/*
+ * VB_DEFSIZE: Default size of buffer of "vb" object. Large sizes may
+ * slow down vbcopy() significantly.
+ */
#define VB_DEFSIZE 256 //>=min of n in vsnprintf()
+
+/*
+ * LOGF_FGETS_BUF: Buffer size for vbgetlinef(), doesn't hard limit anything,
+ * it has only performance impact.
+ */
+#define LOGF_FGETS_BUF VB_DEFSIZE
+
+/*
+ * VB_MAXSIZE: Maximum size of one "vbn" segment to prevent circular
+ * use of "vb" to eat up the whole memory. It doesn't limit the maximum
+ * data storage of "vb" object in no way, it has only performance impact.
+ */
#define VB_MAXSIZE 128*1024 //recommended limit, not hard (probably)
+/*
+ * CHAT_CMD_MAXPARS: Size of chat_cmdpars[] array, maximum # of parameters
+ * a user command can have during chat_proccmd() parse.
+ */
#define CHAT_CMD_MAXPARS 10
/*
* Configuration section ends
****************************/
+#ifndef VB_LOG
+#define VB_LOG(lev,msg...) fprintf(stderr,VB_LOG_##lev ##msg)
+#endif
+#ifndef VB_FATAL
+#define VB_FATAL(lev,msg...) do { VB_LOG(lev, ##msg); exit(EXIT_FAILURE); } while (0)
+#endif
+
+#define VB_LOG_DEBUG "VB_Debug: "
+#define VB_LOG_ERR "VB_Error: "
+#define VB_LOG_CRIT "VB_Critical: "
+
#define strncpydst(d,s) strncpy(d,s,sizeof(d))
#ifdef NDEBUG
#undef DEBUG