From 277e55dae549f6fa6b9ad22d8ebd89af53bcd71b Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 5 Jan 2004 11:31:04 +0000 Subject: [PATCH] idleTime, timeout (0) sets immediate timeout; -1 disabled timeout. proxy forks and checks child exit status and forks again if necessary. --- include/yaz++/proxy.h | 4 +- include/yaz++/socket-manager.h | 10 ++-- include/yaz++/socket-observer.h | 4 +- src/yaz-my-client.cpp | 8 +-- src/yaz-proxy-main.cpp | 106 +++++++++++++++++++++++++++++++++------ src/yaz-proxy.cpp | 4 +- src/yaz-socket-manager.cpp | 21 ++++---- 7 files changed, 116 insertions(+), 41 deletions(-) diff --git a/include/yaz++/proxy.h b/include/yaz++/proxy.h index 8ed5473..45507f4 100644 --- a/include/yaz++/proxy.h +++ b/include/yaz++/proxy.h @@ -1,8 +1,8 @@ /* - * Copyright (c) 1998-2003, Index Data. + * Copyright (c) 1998-2004, Index Data. * See the file LICENSE for details. * - * $Id: proxy.h,v 1.25 2004-01-05 09:31:09 adam Exp $ + * $Id: proxy.h,v 1.26 2004-01-05 11:31:04 adam Exp $ */ #include diff --git a/include/yaz++/socket-manager.h b/include/yaz++/socket-manager.h index 684ff80..f4e8162 100644 --- a/include/yaz++/socket-manager.h +++ b/include/yaz++/socket-manager.h @@ -1,8 +1,8 @@ /* - * Copyright (c) 1998-2000, Index Data. + * Copyright (c) 1998-2004, Index Data. * See the file LICENSE for details. * - * $Id: socket-manager.h,v 1.2 2002-10-23 21:23:01 adam Exp $ + * $Id: socket-manager.h,v 1.3 2004-01-05 11:31:04 adam Exp $ */ #ifndef YAZ_SOCKET_MANAGER_INCLUDED @@ -21,8 +21,8 @@ class YAZ_EXPORT Yaz_SocketManager : public IYazSocketObservable { IYazSocketObserver *observer; int fd; unsigned mask; - unsigned timeout; - unsigned timeout_this; + int timeout; + int timeout_this; time_t last_activity; YazSocketEntry *next; }; @@ -53,7 +53,7 @@ class YAZ_EXPORT Yaz_SocketManager : public IYazSocketObservable { virtual void maskObserver(IYazSocketObserver *observer, int mask); /// Set timeout virtual void timeoutObserver(IYazSocketObserver *observer, - unsigned timeout); + int timeout); /// Process one event. return > 0 if event could be processed; int processEvent(); Yaz_SocketManager(); diff --git a/include/yaz++/socket-observer.h b/include/yaz++/socket-observer.h index 3644893..128d311 100644 --- a/include/yaz++/socket-observer.h +++ b/include/yaz++/socket-observer.h @@ -2,7 +2,7 @@ * Copyright (c) 1998-2000, Index Data. * See the file LICENSE for details. * - * $Id: socket-observer.h,v 1.1 2002-10-09 12:50:26 adam Exp $ + * $Id: socket-observer.h,v 1.2 2004-01-05 11:31:04 adam Exp $ */ #ifndef YAZ_SOCKET_OBSERVER_H @@ -47,7 +47,7 @@ class YAZ_EXPORT IYazSocketObservable { virtual void maskObserver(IYazSocketObserver *observer, int mask) = 0; /// Specify timeout virtual void timeoutObserver(IYazSocketObserver *observer, - unsigned timeout)=0; + int timeout)=0; }; /** Socket Observer. diff --git a/src/yaz-my-client.cpp b/src/yaz-my-client.cpp index cce989d..3597425 100644 --- a/src/yaz-my-client.cpp +++ b/src/yaz-my-client.cpp @@ -2,7 +2,7 @@ * Copyright (c) 1998-2003, Index Data. * See the file LICENSE for details. * - * $Id: yaz-my-client.cpp,v 1.14 2003-10-16 10:26:58 adam Exp $ + * $Id: yaz-my-client.cpp,v 1.15 2004-01-05 11:31:04 adam Exp $ */ #include @@ -454,7 +454,7 @@ int MyClient::cmd_connect(char *host) client (host); timeout (10); wait (); - timeout (0); + timeout (-1); return 1; } @@ -463,7 +463,7 @@ int MyClient::cmd_open(char *host) client (host); timeout (10); wait (); - timeout (0); + timeout (-1); send_initRequest(); wait (); return 1; @@ -676,7 +676,7 @@ int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv) client (host); timeout (10); wait (); - timeout (0); + timeout (-1); send_initRequest(); wait (); } diff --git a/src/yaz-proxy-main.cpp b/src/yaz-proxy-main.cpp index 62bc87e..06df98e 100644 --- a/src/yaz-proxy-main.cpp +++ b/src/yaz-proxy-main.cpp @@ -1,8 +1,8 @@ /* - * Copyright (c) 1998-2003, Index Data. + * Copyright (c) 1998-2004, Index Data. * See the file LICENSE for details. * - * $Id: yaz-proxy-main.cpp,v 1.26 2003-10-24 12:19:23 adam Exp $ + * $Id: yaz-proxy-main.cpp,v 1.27 2004-01-05 11:31:04 adam Exp $ */ #include @@ -27,6 +27,7 @@ void usage(char *prog) static char *pid_fname = 0; static char *uid = 0; static char *log_file = 0; +static int debug = 0; int args(Yaz_Proxy *proxy, int argc, char **argv) { @@ -35,7 +36,8 @@ int args(Yaz_Proxy *proxy, int argc, char **argv) char *prog = argv[0]; int ret; - while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:", argv, argc, &arg)) != -2) + while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:X", + argv, argc, &arg)) != -2) { int err; switch (ret) @@ -89,6 +91,9 @@ int args(Yaz_Proxy *proxy, int argc, char **argv) case 'T': proxy->set_target_idletime(atoi(arg)); break; + case 'X': + debug = 1; + break; case 'p': if (!pid_fname) pid_fname = xstrdup(arg); @@ -104,7 +109,7 @@ int args(Yaz_Proxy *proxy, int argc, char **argv) } if (addr) { - yaz_log(LOG_LOG, "Starting proxy pid=%ld", (long) getpid()); + yaz_log(LOG_LOG, "0 Starting proxy " VERSION ); if (proxy->server(addr)) { yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr); @@ -127,18 +132,10 @@ static void sighup_handler(int num) static_yaz_proxy->reconfig(); } -int main(int argc, char **argv) -{ - static int mk_pid = 0; - Yaz_SocketManager mySocketManager; - Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager)); - - static_yaz_proxy = &proxy; - - signal(SIGHUP, sighup_handler); - - args(&proxy, argc, argv); +static void child_run(Yaz_SocketManager *m) +{ + yaz_log(LOG_LOG, "0 proxy pid=%ld", (long) getpid()); if (pid_fname) { FILE *f = fopen(pid_fname, "w"); @@ -174,9 +171,86 @@ int main(int argc, char **argv) xfree(uid); } - while (mySocketManager.processEvent() > 0) + while (m->processEvent() > 0) ; exit (0); +} + +int main(int argc, char **argv) +{ + int cont = 1; + static int mk_pid = 0; + Yaz_SocketManager mySocketManager; + Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager)); + + static_yaz_proxy = &proxy; + + signal(SIGHUP, sighup_handler); + + args(&proxy, argc, argv); + + if (debug) + { + child_run(&mySocketManager); + exit(0); + } + while (cont) + { + pid_t p = fork(); + if (p == (pid_t) -1) + { + yaz_log(LOG_FATAL|LOG_ERRNO, "fork"); + exit(1); + } + else if (p == 0) + { + child_run(&mySocketManager); + } + pid_t p1; + int status; + p1 = wait(&status); + if (p1 != p) + { + yaz_log(LOG_FATAL, "p1=%d != p=%d", p1, p); + exit(1); + } + if (WIFSIGNALED(status)) + { + switch(WTERMSIG(status)) { + case SIGILL: + yaz_log(LOG_WARN, "Received SIGILL from child %ld", (long) p); + cont = 1; + break; + case SIGABRT: + yaz_log(LOG_WARN, "Received SIGABRT from child %ld", (long) p); + cont = 1; + break ; + case SIGSEGV: + yaz_log(LOG_WARN, "Received SIGSEGV from child %ld", (long) p); + cont = 1; + break; + case SIGTERM: + yaz_log(LOG_LOG, "Received SIGTERM from child %ld", + WTERMSIG(status), (long) p); + cont = 0; + break; + default: + yaz_log(LOG_WARN, "Received SIG %d from child %ld", + WTERMSIG(status), (long) p); + cont = 0; + } + } + else if (status == 0) + cont = 0; + else + { + yaz_log(LOG_LOG, "Exit %d from child %ld", status, (long) p); + cont = 1; + } + if (cont) + sleep(1); + } + exit (0); return 0; } diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index 4e97876..7da3e15 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -1,8 +1,8 @@ /* - * Copyright (c) 1998-2003, Index Data. + * Copyright (c) 1998-2004, Index Data. * See the file LICENSE for details. * - * $Id: yaz-proxy.cpp,v 1.75 2004-01-05 09:31:09 adam Exp $ + * $Id: yaz-proxy.cpp,v 1.76 2004-01-05 11:31:04 adam Exp $ */ #include diff --git a/src/yaz-socket-manager.cpp b/src/yaz-socket-manager.cpp index 82bc0a3..3215d3b 100644 --- a/src/yaz-socket-manager.cpp +++ b/src/yaz-socket-manager.cpp @@ -1,8 +1,8 @@ /* - * Copyright (c) 1998-2003, Index Data. + * Copyright (c) 1998-2004, Index Data. * See the file LICENSE for details. * - * $Id: yaz-socket-manager.cpp,v 1.22 2003-12-16 11:26:42 adam Exp $ + * $Id: yaz-socket-manager.cpp,v 1.23 2004-01-05 11:31:04 adam Exp $ */ #include #ifdef WIN32 @@ -44,7 +44,7 @@ void Yaz_SocketManager::addObserver(int fd, IYazSocketObserver *observer) se->fd = fd; se->mask = 0; se->last_activity = 0; - se->timeout = 0; + se->timeout = -1; } void Yaz_SocketManager::deleteObserver(IYazSocketObserver *observer) @@ -87,7 +87,7 @@ void Yaz_SocketManager::maskObserver(IYazSocketObserver *observer, int mask) } void Yaz_SocketManager::timeoutObserver(IYazSocketObserver *observer, - unsigned timeout) + int timeout) { YazSocketEntry *se; @@ -100,7 +100,7 @@ int Yaz_SocketManager::processEvent() { YazSocketEntry *p; YazSocketEvent *event = getEvent(); - unsigned timeout = 0; + int timeout = -1; yaz_log (m_log, "Yaz_SocketManager::processEvent manager=%p", this); if (event) { @@ -141,7 +141,7 @@ int Yaz_SocketManager::processEvent() } if (fd > max) max = fd; - if (p->timeout) + if (p->timeout >= 0) { unsigned timeout_this; timeout_this = p->timeout; @@ -149,9 +149,9 @@ int Yaz_SocketManager::processEvent() timeout_this -= now - p->last_activity; else p->last_activity = now; - if (timeout_this < 1 || timeout_this > 2147483646) - timeout_this = 1; - if (!timeout || timeout_this < timeout) + if (timeout_this < 0 || timeout_this > 2147483646) + timeout_this = 0; + if (timeout == -1 || timeout_this < timeout) timeout = timeout_this; p->timeout_this = timeout_this; yaz_log (m_log, "Yaz_SocketManager::select timeout_this=%d", @@ -172,7 +172,8 @@ int Yaz_SocketManager::processEvent() yaz_log (m_log, "Yaz_SocketManager::select begin no=%d timeout=%d", no, timeout); - while ((res = select(max + 1, &in, &out, &except, timeout ? &to : 0)) < 0) + while ((res = select(max + 1, &in, &out, &except, + timeout== -1 ? 0 : &to)) < 0) if (errno != EINTR) { yaz_log (LOG_LOG|LOG_WARN, "select"); -- 1.7.10.4