crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

commit 780d4f60c8cc41e012992b78e7e7349653b7077c
parent 43053c5d479561e3ebab14caa88bdbbb460ed0a6
Author: finwo <finwo@pm.me>
Date:   Sat,  3 Jan 2026 19:35:07 +0100

2.06

Diffstat:
MChangeLog | 7+++++++
MMakefile | 4++--
Mxr/Checkers/checkupthread/checkupthread | 2+-
Mxr/Checkers/wakeupthread/wakeupthread | 2+-
Mxr/DispatchAlgorithms/algorithm/algorithm | 2+-
Mxr/DispatchAlgorithms/roundrobin/target.cc | 14+++++++++-----
Mxr/Makefile | 31++++++++++++++++++++-----------
Axr/ThreadsAndMutexes/mutex/lock.cc | 9+++++++++
Axr/ThreadsAndMutexes/mutex/mutex | 19+++++++++++++++++++
Axr/ThreadsAndMutexes/mutex/plock.cc | 17+++++++++++++++++
Axr/ThreadsAndMutexes/mutex/unlock.cc | 8++++++++
Rxr/thread/execute.cc -> xr/ThreadsAndMutexes/thread/execute.cc | 0
Rxr/thread/start.cc -> xr/ThreadsAndMutexes/thread/start.cc | 0
Axr/ThreadsAndMutexes/thread/thread | 18++++++++++++++++++
Rxr/thread/thread2.cc -> xr/ThreadsAndMutexes/thread/thread2.cc | 0
Axr/backend/addbytes.cc | 7+++++++
Mxr/backend/backend | 23++++++++++++-----------
Mxr/backend/backend1.cc | 6+++++-
Mxr/backend/backend2.cc | 2+-
Axr/backend/endconnection.cc | 7+++++++
Mxr/backend/live.cc | 3+++
Axr/backend/startconnection.cc | 11+++++++++++
Mxr/balancer/serve.cc | 3++-
Mxr/config/parsecmdline.cc | 29++++++++++++++++++++++++++---
Mxr/etc/Makefile.class | 4+++-
Mxr/etc/c-conf | 39++++++++++++++++++++++++++++-----------
Mxr/etc/usage.txt | 4+++-
Mxr/httpbuffer/bodyreceived.cc | 6+++---
Mxr/httpdispatcher/dispatch.cc | 2--
Mxr/sys/debugmsg.cc | 9++++-----
Axr/sys/inetaton.cc | 9+++++++++
Mxr/sys/ipmatch.cc | 2+-
Mxr/sys/main.cc | 7+++++--
Mxr/sys/msg.cc | 9++++-----
Mxr/sys/reportmsg.cc | 9++++-----
Mxr/sys/sys | 12++++++++++++
Mxr/tcpdispatcher/dispatch.cc | 2--
Mxr/tcpdispatcher/execute.cc | 4----
Mxr/tcpdispatcher/tcpdispatcher | 2+-
Dxr/thread/lock.cc | 13-------------
Dxr/thread/thread | 23-----------------------
Dxr/thread/thread1.cc | 6------
Dxr/thread/unlock.cc | 7-------
Mxrctl/xrctl | 4+++-
44 files changed, 266 insertions(+), 131 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,10 @@ +2.06 [KK 2008-08-27] +Upped c-conf to 1.14. +xrctl updated: 'ps' command format also suitable for SunOS. +New class Mutex implemented. Mutex-locks are now more fine-grained +(per one global or static). +Solaris9 port w/gcc 3.4.6 done. + 2.05 [KK 2008-08-15] Thrown errors now based on std::exeception. "xr -V" shows compilation settings. diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ # Top-level Makefile for XR # ------------------------- -VER = 2.05 +VER = 2.06 BINDIR = /usr/sbin TAR = /tmp/crossroads-$(VER).tar.gz AUTHOR = Karel Kubat <karel@kubat.nl> @@ -44,7 +44,7 @@ tar: rm -rf $(TAR) /tmp/crossroads-$(VER) cd ..; cp -r crossroads /tmp/crossroads-$(VER) cd /tmp; tar czf $(TAR) \ - --exclude .git --exclude crossroads-$(VER)/xr/build \ + --exclude .git --exclude .svn --exclude crossroads-$(VER)/xr/build \ crossroads-$(VER) @echo @echo 'Sources now tarred into $(TAR)' diff --git a/xr/Checkers/checkupthread/checkupthread b/xr/Checkers/checkupthread/checkupthread @@ -2,7 +2,7 @@ #define _CHECKUPTHREAD_ #include "sys/sys" -#include "thread/thread" +#include "ThreadsAndMutexes/thread/thread" #include "balancer/balancer" #include "error/error" diff --git a/xr/Checkers/wakeupthread/wakeupthread b/xr/Checkers/wakeupthread/wakeupthread @@ -2,7 +2,7 @@ #define _WAKEUPTHREAD_ #include "sys/sys" -#include "thread/thread" +#include "ThreadsAndMutexes/thread/thread" #include "balancer/balancer" #include "error/error" diff --git a/xr/DispatchAlgorithms/algorithm/algorithm b/xr/DispatchAlgorithms/algorithm/algorithm @@ -1,7 +1,7 @@ #ifndef _ALGORITHM_ #define _ALGORITHM_ -#include "thread/thread" +#include "ThreadsAndMutexes/thread/thread" class Algorithm: public Thread { public: diff --git a/xr/DispatchAlgorithms/roundrobin/target.cc b/xr/DispatchAlgorithms/roundrobin/target.cc @@ -5,21 +5,25 @@ int Roundrobin::target(struct in_addr clientip) { int t = last + 1; if (last == -1) { - lock(); + // Initiaizer code + lock(&last); last = 0; - unlock(); + unlock(&last); } while (1) { if (balancer.backend(t).available()) { - lock(); + lock(&last); last = t; - unlock(); + unlock(&last); return (t); } t++; t %= balancer.nbackends(); - if (t == last) + lock (&last); + bool came_around = t == last; + unlock (&last); + if (came_around) throw static_cast<Error> ("Round-robin algorithm: no available back ends"); } diff --git a/xr/Makefile b/xr/Makefile @@ -1,13 +1,20 @@ # Configuration -DIRS = $(shell find . -mindepth 1 -type d | grep -v .svn) -BUILDDIR = build -BIN = $(BUILDDIR)/xr -LIB = $(BUILDDIR)/libxr.a -TMPXR = /tmp/xr-$(shell whoami) -CONF_CC = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache c++-compiler) -CONF_LIB = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache lib \ - ucb nsl pthread socket m alf) +DIRS = $(shell find . -type d | grep -v '.svn') +BUILDDIR = build +BIN = $(BUILDDIR)/xr +LIB = $(BUILDDIR)/libxr.a +TMPXR = /tmp/xr-$(shell whoami) +CONF_CC = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache c++-compiler) +CONF_LIB = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache \ + lib ucb nsl pthread socket m alf) +CONF_GETOPT = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache \ + ifheader getopt.h HAVE_GETOPT_H) +CONF_GETOPT_LONG = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache \ + libfunction getopt_long HAVE_GETOPT_LONG) +CONF_INET_ATON = $(shell etc/c-conf -vc $(BUILDDIR)/config.cache \ + libfunction inet_aton HAVE_INET_ATON) + foo: $(MAKE) subdirs $(MAKE) $(BIN) @@ -20,13 +27,15 @@ $(BINDIR)/xr: $(BUILDDIR)/xr rm -f $(TMPXR) subdirs: $(BUILDDIR)/usage.h - @for f in $(DIRS) ; \ - do \ + @echo 'About to build in class dirs: $(DIRS)' + @for f in $(DIRS) ; do \ echo "Making: $$f"; \ BASE=$(BASE) CC=$(CONF_CC) BUILDDIR=$(BUILDDIR) VER='$(VER)' \ AUTHOR='$(AUTHOR)' MAINTAINER='$(MAINTAINER)' \ CONF_CC='$(CONF_CC)' CONF_LIB='$(CONF_LIB)' \ - $(MAKE) -C $$f -f $(BASE)/xr//etc/Makefile.class \ + CONF_GETOPT=$(CONF_GETOPT) CONF_GETOPT_LONG=$(CONF_GETOPT_LONG) \ + CONF_INET_ATON=$(CONF_INET_ATON) \ + $(MAKE) -C $$f -f $(BASE)/xr/etc/Makefile.class \ || exit 1; \ done ar rs $(LIB) */*.o diff --git a/xr/ThreadsAndMutexes/mutex/lock.cc b/xr/ThreadsAndMutexes/mutex/lock.cc @@ -0,0 +1,9 @@ +#include "mutex" + +std::map<void *, pthread_mutex_t> Mutex::s_lock; + +void Mutex::lock (void *target) { + plock(&s_lock); + plock(target); + unlock(&s_lock); +} diff --git a/xr/ThreadsAndMutexes/mutex/mutex b/xr/ThreadsAndMutexes/mutex/mutex @@ -0,0 +1,19 @@ +#ifndef _MUTEX_ +#define _MUTEX_ + +#include "sys/sys" + +class Mutex { +public: + static void lock (void *target); + static void unlock (void *target); + +private: + static void plock (void *target); + static std::map<void *, pthread_mutex_t> s_lock; + typedef std::map<void *, pthread_mutex_t>::iterator mapIterator; +}; + + + +#endif diff --git a/xr/ThreadsAndMutexes/mutex/plock.cc b/xr/ThreadsAndMutexes/mutex/plock.cc @@ -0,0 +1,17 @@ +#include "mutex" +#include "error/error" + +void Mutex::plock(void *target) { + mapIterator iter = s_lock.find(target); + if (iter == s_lock.end()) { + // No such lock yet, create the mutex + if (int res = pthread_mutex_init(&s_lock[target], 0)) + throw static_cast<Error>("Failed to initialize static mutex: ") + + strerror(res); + } + + if (int res = pthread_mutex_lock(&s_lock[target])) + throw static_cast<Error>("Failed to obtain mutex lock: ") + + strerror(res); +} + diff --git a/xr/ThreadsAndMutexes/mutex/unlock.cc b/xr/ThreadsAndMutexes/mutex/unlock.cc @@ -0,0 +1,8 @@ +#include "mutex" +#include "error/error" + +void Mutex::unlock(void *target) { + if (int res = pthread_mutex_unlock(&s_lock[target])) + throw static_cast<Error>("Failed to release mutex lock: ") + + strerror(res); +} diff --git a/xr/thread/execute.cc b/xr/ThreadsAndMutexes/thread/execute.cc diff --git a/xr/thread/start.cc b/xr/ThreadsAndMutexes/thread/start.cc diff --git a/xr/ThreadsAndMutexes/thread/thread b/xr/ThreadsAndMutexes/thread/thread @@ -0,0 +1,18 @@ +#ifndef _THREAD_ +#define _THREAD_ + +#include "sys/sys" +#include "error/error" +#include "config/config" +#include "ThreadsAndMutexes/mutex/mutex" + +using namespace std; + +class Thread: public Mutex { +public: + virtual ~Thread(); + void start(); + virtual void execute(); +}; + +#endif diff --git a/xr/thread/thread2.cc b/xr/ThreadsAndMutexes/thread/thread2.cc diff --git a/xr/backend/addbytes.cc b/xr/backend/addbytes.cc @@ -0,0 +1,7 @@ +#include "backend" + +void Backend::addbytes (unsigned n) { + Mutex::lock (&bytes_served); + bytes_served += n; + Mutex::unlock (&bytes_served); +} diff --git a/xr/backend/backend b/xr/backend/backend @@ -1,10 +1,11 @@ #ifndef _BACKEND_ #define _BACKEND_ -#include "../sys/sys" -#include "../backenddef/backenddef" -#include "../fdset/fdset" -#include "../error/error" +#include "sys/sys" +#include "backenddef/backenddef" +#include "fdset/fdset" +#include "error/error" +#include "ThreadsAndMutexes/mutex/mutex" using namespace std; @@ -27,11 +28,12 @@ public: int port() const { return (bdef.port()); } unsigned maxconn() const { return (bdef.maxconn()); } unsigned connections() const { return (nconn); } - void startconnection() { nconn++; totconn++; } - void endconnection() { nconn--; } - void addbytes (unsigned n) { bytes_served += n; } double bytesserved() const { return (bytes_served); } unsigned clientsserved() const { return (totconn); } + + static void addbytes (unsigned n); + static void startconnection(); + static void endconnection(); BackendDef const &backenddef() const { return (bdef); @@ -39,11 +41,10 @@ public: private: BackendDef bdef; - bool islive; + static bool islive; int clsocket; - unsigned nconn; - unsigned totconn; - double bytes_served; + static unsigned nconn, totconn; + static double bytes_served; }; #endif diff --git a/xr/backend/backend1.cc b/xr/backend/backend1.cc @@ -1,5 +1,9 @@ #include "backend" +bool Backend::islive = true; +unsigned Backend::nconn = 0, Backend::totconn = 0; +double Backend::bytes_served = 0; + Backend::Backend () : - islive(true), clsocket(-1), nconn(0), totconn(0), bytes_served(0) { + clsocket(-1) { } diff --git a/xr/backend/backend2.cc b/xr/backend/backend2.cc @@ -1,5 +1,5 @@ #include "backend" Backend::Backend (BackendDef const &b) : - bdef(b), islive(true), clsocket(-1), nconn(0), totconn(0), bytes_served(0) { + bdef(b), clsocket(-1) { } diff --git a/xr/backend/endconnection.cc b/xr/backend/endconnection.cc @@ -0,0 +1,7 @@ +#include "backend" + +void Backend::endconnection() { + Mutex::lock (&nconn); + nconn--; + Mutex::unlock (&nconn); +} diff --git a/xr/backend/live.cc b/xr/backend/live.cc @@ -1,8 +1,11 @@ #include "backend" void Backend::live (bool state) { + Mutex::lock (&islive); bool oldstate = islive; islive = state; + Mutex::unlock (&islive); + if (oldstate != state) msg ("Marking back end " + description() + " as " + livestr() + "\n"); } diff --git a/xr/backend/startconnection.cc b/xr/backend/startconnection.cc @@ -0,0 +1,11 @@ +#include "backend" + +void Backend::startconnection() { + Mutex::lock (&nconn); + nconn++; + Mutex::unlock (&nconn); + + Mutex::lock (&totconn); + totconn++; + Mutex::unlock (&totconn); +} diff --git a/xr/balancer/serve.cc b/xr/balancer/serve.cc @@ -185,9 +185,10 @@ void Balancer::serve() { d->start(); } else { // If fd-serving, serve and close. Don't thread it up. - TcpDispatcher *d; + TcpDispatcher *d; struct in_addr dummy; inet_aton ("0.0.0.0", &dummy); + switch (config.stype()) { case Servertype::t_tcp: d = new TcpDispatcher (server_fd, dummy); diff --git a/xr/config/parsecmdline.cc b/xr/config/parsecmdline.cc @@ -16,6 +16,8 @@ void Config::parsecmdline (int ac, char **av) { throw static_cast<Error>("Bad command line '") + cmdline + "'\n" + USAGE; +# define OPTSTRING "?a:A:B:b:c:Dd:fhH:m:np:Ss:t:T:vVw:xX" +# ifdef HAVE_GETOPT_LONG static struct option longopts[] = { { "allow-from", required_argument, 0, 'a' }, { "deny-from", required_argument, 0, 'A' }, @@ -43,13 +45,18 @@ void Config::parsecmdline (int ac, char **av) { { "add-x-forwarded-for", no_argument, 0, 'x' }, { 0, 0, 0, 0 } }; +# endif int opt; bool backend_set = false; bool tryout = false; - while ( (opt = getopt_long (ac, av, - "?a:A:B:b:c:Dd:fhH:m:np:Ss:t:T:vVw:xX", - longopts, 0)) > 0) { + +# ifdef HAVE_GETOPT_LONG + while ( (opt = getopt_long (ac, av, OPTSTRING, longopts, 0)) > 0) +#else + while ( (opt = getopt (ac, av, OPTSTRING)) > 0 ) +#endif + { switch (opt) { case 'a': addallow (optarg); @@ -121,6 +128,22 @@ void Config::parsecmdline (int ac, char **av) { << "C++ compiler : " << CONF_CC << "\n" << "Libraries : " << CONF_LIB << "\n" ; +# ifdef HAVE_GETOPT_H + cout << "getopt.h : present\n"; +# else + cout << "getopt.h : absent\n"; +# endif +# ifdef HAVE_GETOPT_LONG + cout << "getopt_long() : present\n"; +# else + cout << "getopt_long() : absent (only short flags will work)\n"; +# endif +# ifdef HAVE_INADDR_NONE + cout << "INADDR_NONE : present\n"; +# else + cout << "INADDR_NONE : absent, defined to " << INADDR_NONE + << "\n"; +# endif exit (0); case 'w': wakeup = setinteger (optarg); diff --git a/xr/etc/Makefile.class b/xr/etc/Makefile.class @@ -5,8 +5,10 @@ DIR = $(shell pwd | sed 's:.*/::') class-compile: $(OBJ) $(BASE)/xr/$(BUILDDIR)/$(DIR)_%.o: %.cc - $(CC) -DVER='"$(VER)"' -DAUTHOR='"$(AUTHOR)"' \ + @echo "Compiling: " `pwd` $< + @$(CONF_CC) -DVER='"$(VER)"' -DAUTHOR='"$(AUTHOR)"' \ -DMAINTAINER='"$(MAINTAINER)"' \ -DCONF_CC='"$(CONF_CC)"' -DCONF_LIB='"$(CONF_LIB)"' \ + $(CONF_GETOPT) $(CONF_GETOPT_LONG) $(CONF_INET_ATON) \ -I$(BASE)/xr \ -c -g -Wall -o $@ $< diff --git a/xr/etc/c-conf b/xr/etc/c-conf @@ -4,7 +4,10 @@ use strict; use Getopt::Std; # Globals -my $VER = "1.13"; +my $VER = "1.14"; +# 1.14 [KK 2008-08-22] c-compiler and c++-compiler attempt to find by +# version, eg. '/opt/local/bin/g++-mp-4.2' is better +# than '/usr/bin/g++' # 1.13 [KK 2008-07-15] Opimized subfiles() - way faster when nonrecursive now # 1.12 [KK 2008-04-15] Messaging improved upon -v flag # 1.11 [KK 2008-01-14] Added /opt/local/{lib,include} to the standard libs. @@ -151,13 +154,25 @@ sub uname() { # Find a binary along the path. sub findbin($) { my $bin = shift; + my $bestx = undef; + my $bestver = 0; foreach my $d (split (/:/, $ENV{PATH})) { - if (-x "$d/$bin" or -f "$d/bin.exe") { - msg ("Found executable '$bin' as '$d/$bin'\n"); - return ("$d/$bin"); + for my $x (glob("$d/$bin"), glob("$d/$bin.exe"), + glob("$d/$bin-*"), glob("$d/$bin-*.exe")) { + if (-x $x) { + my $ver = $x; + $ver =~ s{^.*/[^\d]*}{}; + $ver = sprintf("%g", $ver); + # msg ("Candidate for '$bin': $x, version $ver\n"); + if ($bestver < $ver or !$bestx) { + $bestver = $ver; + $bestx = $x; + } + } } } - msg ("Failed to locate executable '$bin'!\n"); + msg ("Failed to locate executable '$bin'!\n") unless ($bestx); + return ($bestx); } # Recursively determine the files under a given dir. @@ -399,7 +414,8 @@ ENDHELP # Find the C compiler and return it, or die trying. sub find_c_compiler { foreach my $c (@c_compilers) { - return ($c) if (findbin ($c)); + my $full = findbin($c); + return ($full) if ($full); } die ("No C compiler found\n"); } @@ -433,9 +449,10 @@ ENDHELP usage() if ($#_ > -1); foreach my $c (@cpp_compilers) { - if (findbin ($c)) { - msg ("C++ compiler: '$c'\n"); - output ($c); + my $full = findbin($c); + if ($full) { + msg ("C++ compiler: '$full'\n"); + output ($full); return; } } @@ -486,10 +503,10 @@ E.g.: $base libfunction printf HAVE_PRINTF ENDHELP if (test_libfunction (@_)) { - msg ("Library function '$_[1]' present\n"); + msg ("Library function '$_[0]' present\n"); output ("-D$_[1]=1"); } else { - msg ("Library function '$_[1]' absent\n"); + msg ("Library function '$_[0]' absent\n"); } } diff --git a/xr/etc/usage.txt b/xr/etc/usage.txt @@ -1,6 +1,8 @@ This is XR, a load balancer and failover utility for TCP/HTTP services. -Usage: xr [flags], where the flags may be: +Usage: xr [flags], where the flags may be the following (long versions +may not exist on your platform): + -a MASK, --allow-from MASK Allow only clients that match MASK. MASK is e.g. 192.168.255.255, which would allow the class B network 192.168.*.* diff --git a/xr/httpbuffer/bodyreceived.cc b/xr/httpbuffer/bodyreceived.cc @@ -1,9 +1,9 @@ #include "httpbuffer" static bool ishex (char ch) { - return (ch >= '0' && ch <= '9' || - ch >= 'a' && ch <= 'f' || - ch >= 'A' && ch <= 'F'); + return ( (ch >= '0' && ch <= '9') || + (ch >= 'a' && ch <= 'f') || + (ch >= 'A' && ch <= 'F') ); } Httpbuffer::Bodystatus Httpbuffer::bodyreceived() { diff --git a/xr/httpdispatcher/dispatch.cc b/xr/httpdispatcher/dispatch.cc @@ -22,9 +22,7 @@ void HttpDispatcher::dispatch() { Backend tb = balancer.backend(stickytarget); msg ("Sticky HTTP request for " + tb.description() + "\n"); if (! tb.connect()) { - lock(); balancer.backend(stickytarget).live(false); - unlock(); msg ("Failed to connect to back end " + tb.description() + ", trying to dispatch to other\n"); issticky(false); diff --git a/xr/sys/debugmsg.cc b/xr/sys/debugmsg.cc @@ -1,15 +1,14 @@ #include "sys" -#include "../config/config" -#include "../thread/thread" +#include "config/config" +#include "ThreadsAndMutexes/mutex/mutex" void debugmsg (string const &s) { if (config.debug()) { - Thread th; - th.lock(); + Mutex::lock(&cerr); if (config.prefixtimestamp()) cerr << timestamp() << ' '; cerr << pthread_self() << " DEBUG: " << s; cerr.flush(); - th.unlock(); + Mutex::unlock(&cerr); } } diff --git a/xr/sys/inetaton.cc b/xr/sys/inetaton.cc @@ -0,0 +1,9 @@ +#include "sys" + +#ifndef HAVE_INET_ATON +int inet_aton (char const *name, struct in_addr *addr) { + in_addr_t a = inet_addr (name); + addr->s_addr = a; + return a != (in_addr_t)-1; +} +#endif diff --git a/xr/sys/ipmatch.cc b/xr/sys/ipmatch.cc @@ -4,7 +4,7 @@ bool ipmatch (struct in_addr adr, struct in_addr mask) { long laddr = * ((long*)&adr); long lmask = * ((long*)&mask); - bool match = laddr & lmask == laddr; + bool match = ( (laddr & lmask) == laddr ); if (config.debug()) debugmsg ("Matching ip " + (string)inet_ntoa(adr) + " against mask " + diff --git a/xr/sys/main.cc b/xr/sys/main.cc @@ -30,9 +30,12 @@ int main (int argc, char **argv) { msg ("XR running as PID " + o.str() + "\n"); // Load the signal handler. - for (unsigned i = 0; i < sizeof(relevant_sig); i++) + for (unsigned i = 0; i < sizeof(relevant_sig) / sizeof(int); i++) { + cout << relevant_sig[i] << "\n"; signal (relevant_sig[i], sigcatcher); - + } + cout << "ok\n"; + // Configure the balancer and start serving. balancer.init(); balancer.serve(); diff --git a/xr/sys/msg.cc b/xr/sys/msg.cc @@ -1,15 +1,14 @@ #include "sys" -#include "../config/config" -#include "../thread/thread" +#include "config/config" +#include "ThreadsAndMutexes/mutex/mutex" void msg (string const &s) { if (config.verbose()) { - Thread th; - th.lock(); + Mutex::lock(&cerr); if (config.prefixtimestamp()) cerr << timestamp() << ' '; cerr << pthread_self() << " INFO: " << s; cerr.flush(); - th.unlock(); + Mutex::unlock(&cerr); } } diff --git a/xr/sys/reportmsg.cc b/xr/sys/reportmsg.cc @@ -1,13 +1,12 @@ #include "sys" -#include "../config/config" -#include "../thread/thread" +#include "config/config" +#include "ThreadsAndMutexes/mutex/mutex" void reportmsg (string const &s) { - Thread th; - th.lock(); + Mutex::lock(&cerr); if (config.prefixtimestamp()) cerr << timestamp() << ' '; cerr << pthread_self() << " REPORT: " << s; cerr.flush(); - th.unlock(); + Mutex::unlock(&cerr); } diff --git a/xr/sys/sys b/xr/sys/sys @@ -8,7 +8,9 @@ // C #include <errno.h> #include <fcntl.h> +#ifdef HAVE_GETOPT_H #include <getopt.h> +#endif #include <netdb.h> #include <signal.h> #include <stdio.h> @@ -23,6 +25,12 @@ #include <sys/time.h> #include <sys/types.h> +#ifdef INADDR_NONE +# define HAVE_INADDR_NONE +#else +# define INADDR_NONE 0xffffffff +#endif + // C++ #include <exception> #include <iostream> @@ -43,4 +51,8 @@ string timestamp(time_t s = 0); bool ipmatch (struct in_addr addr, struct in_addr mask); void socketclose (int fd); +#ifndef HAVE_INET_ATON +int inet_aton (char const *name, struct in_addr *addr); +#endif + #endif diff --git a/xr/tcpdispatcher/dispatch.cc b/xr/tcpdispatcher/dispatch.cc @@ -8,9 +8,7 @@ void TcpDispatcher::dispatch() { target_backend = algorithm->target(clientip()); tb = balancer.backend(target_backend); if (!tb.connect()) { - lock(); balancer.backend(target_backend).live(false); - unlock(); msg ("Failed to connect to back end " + tb.description() + ", trying to dispatch to other\n"); } else { diff --git a/xr/tcpdispatcher/execute.cc b/xr/tcpdispatcher/execute.cc @@ -21,9 +21,7 @@ void TcpDispatcher::execute() { balancer.backend(target_backend).description() + ", fd " + bo.str() + "\n"); - lock(); balancer.backend(target_backend).startconnection(); - unlock(); try { handle(); @@ -31,9 +29,7 @@ void TcpDispatcher::execute() { cerr << e.what() << "\n"; } - lock(); balancer.backend(target_backend).endconnection(); - unlock(); socketclose (clientfd()); socketclose (backendfd()); diff --git a/xr/tcpdispatcher/tcpdispatcher b/xr/tcpdispatcher/tcpdispatcher @@ -4,7 +4,7 @@ #include "sys/sys" #include "balancer/balancer" #include "config/config" -#include "thread/thread" +#include "ThreadsAndMutexes/thread/thread" // Dispatching algorithm workers #include "DispatchAlgorithms/algorithm/algorithm" diff --git a/xr/thread/lock.cc b/xr/thread/lock.cc @@ -1,13 +0,0 @@ -#include "thread" - -void Thread::lock() { - if (!initialized) { - if (int res = pthread_mutex_init (&thread_mutex, 0)) - throw static_cast<Error>("Failed to initialize mutex: ") + - strerror(res); - initialized = true; - } - if (int res = pthread_mutex_lock (&thread_mutex)) - throw static_cast<Error>("Failed to obtain mutex lock: ") + - strerror(res); -} diff --git a/xr/thread/thread b/xr/thread/thread @@ -1,23 +0,0 @@ -#ifndef _THREAD_ -#define _THREAD_ - -#include "../sys/sys" -#include "../error/error" -#include "../config/config" - -using namespace std; - -class Thread { -public: - Thread(); - virtual ~Thread(); - void start(); - void lock(); - void unlock(); - virtual void execute(); -private: - static pthread_mutex_t thread_mutex; - static bool initialized; -}; - -#endif diff --git a/xr/thread/thread1.cc b/xr/thread/thread1.cc @@ -1,6 +0,0 @@ -#include "thread" - -pthread_mutex_t Thread::thread_mutex; -bool Thread::initialized = false; -Thread::Thread() { -} diff --git a/xr/thread/unlock.cc b/xr/thread/unlock.cc @@ -1,7 +0,0 @@ -#include "thread" - -void Thread::unlock() { - if (int res = pthread_mutex_unlock (&thread_mutex)) - throw static_cast<Error>("Failed to release mutex lock: ") + - strerror(res); -} diff --git a/xrctl/xrctl b/xrctl/xrctl @@ -7,8 +7,10 @@ use strict; # Directory where PID stamp files are stored, named xr-{service}.pid my $piddir = '/var/run'; -# 'ps' command that prints a process ID and the invoking command +# 'ps' command that prints a process ID and the invoking command. The +# following is right for Linux, MacOSX (Darwin) and Solaris. my $pscmd = '/bin/ps ax -o pid,command'; +$pscmd = '/usr/bin/ps -ef "pid comm"' if (`uname` =~ /SunOS/); # Use 'logger' to send output to syslog? 0 means no. my $use_logger = 1;