From ccc2e6cfe3a5936b0c72fa57faca5fb7b46c0b1d Mon Sep 17 00:00:00 2001 From: Fritz Elfert Date: Sun, 4 Jul 1999 19:12:50 +0000 Subject: Forking, syslog and better error handling --- ncpd/link.cc | 4 ++++ ncpd/main.cc | 31 ++++++++++++++++++++----------- ncpd/ncp.cc | 5 ++++- ncpd/packet.cc | 25 +++++++++++++++++++++++++ ncpd/packet.h | 1 + 5 files changed, 54 insertions(+), 12 deletions(-) (limited to 'ncpd') diff --git a/ncpd/link.cc b/ncpd/link.cc index cc3781d..94a6f97 100644 --- a/ncpd/link.cc +++ b/ncpd/link.cc @@ -159,6 +159,10 @@ poll() return ret; } } + if (p->linkFailed()) { + failed = true; + return ret; + } if (!somethingToSend) { countToResend = 0; diff --git a/ncpd/main.cc b/ncpd/main.cc index 1dc360f..7b46449 100644 --- a/ncpd/main.cc +++ b/ncpd/main.cc @@ -76,7 +76,7 @@ pollSocketConnections(int &numScp, socketChan ** scp) void usage() { - cerr << "Usage : ncpd [-V] [-v logclass] [-p ] [-d ] [-b ]\n"; + cerr << "Usage : ncpd [-V] [-v logclass] [-d] [-p ] [-s ] [-b ]\n"; exit(1); } @@ -86,6 +86,7 @@ main(int argc, char **argv) ppsocket skt; IOWatch iow; int pid; + bool dofork = true; // Command line parameter processing int sockNum = DPORT; @@ -99,7 +100,7 @@ main(int argc, char **argv) for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-p") && i + 1 < argc) { sockNum = atoi(argv[++i]); - } else if (!strcmp(argv[i], "-d") && i + 1 < argc) { + } else if (!strcmp(argv[i], "-s") && i + 1 < argc) { serialDevice = argv[++i]; } else if (!strcmp(argv[i], "-v") && i + 1 < argc) { i++; @@ -119,6 +120,8 @@ main(int argc, char **argv) verbose = true; } else if (!strcmp(argv[i], "-b") && i + 1 < argc) { baudRate = atoi(argv[++i]); + } else if (!strcmp(argv[i], "-d")) { + dofork = 0; } else if (!strcmp(argv[i], "-V")) { cout << "ncpd version " << VERSION << endl; exit(0); @@ -126,18 +129,24 @@ main(int argc, char **argv) usage(); } - switch ((pid = fork())) { + if (dofork) + pid = fork(); + else + pid = 0; + switch (pid) { case 0: if (!skt.listen("127.0.0.1", sockNum)) cerr << "listen on port " << sockNum << ": " << strerror(errno) << endl; else { - logbuf dlog(LOG_DEBUG); - logbuf elog(LOG_ERR); - ostream lout(&dlog); - ostream lerr(&elog); - cout = lout; - cerr = lerr; - openlog("ncpd", LOG_CONS|LOG_PID, LOG_DAEMON); + if (dofork) { + logbuf dlog(LOG_DEBUG); + logbuf elog(LOG_ERR); + ostream lout(&dlog); + ostream lerr(&elog); + cout = lout; + cerr = lerr; + openlog("ncpd", LOG_CONS|LOG_PID, LOG_DAEMON); + } ncp *a = new ncp(serialDevice, baudRate, iow); int numScp = 0; socketChan *scp[8]; @@ -157,7 +166,7 @@ main(int argc, char **argv) if (a->stuffToSend()) iow.watch(0, 100000); else - iow.watch(100000, 0); + iow.watch(1, 0); if (a->hasFailed()) { if (verbose) diff --git a/ncpd/ncp.cc b/ncpd/ncp.cc index 2391586..90b188b 100644 --- a/ncpd/ncp.cc +++ b/ncpd/ncp.cc @@ -51,8 +51,11 @@ ncp::~ncp() void ncp:: reset() { - for (int i = 0; i < 8; i++) + for (int i = 0; i < 8; i++) { + if (channelPtr[i]) + channelPtr[i]->terminateWhenAsked(); channelPtr[i] = NULL; + } failed = false; gotLinkChan = false; l->reset(); diff --git a/ncpd/packet.cc b/ncpd/packet.cc index 70006bc..9009f86 100644 --- a/ncpd/packet.cc +++ b/ncpd/packet.cc @@ -29,6 +29,7 @@ #include #include #include +#include extern "C" { #include "mp_serial.h" @@ -155,6 +156,8 @@ bool packet:: get(unsigned char &type, bufferStore & ret) { while (!terminated()) { + if (linkFailed()) + return false; int res = read(fd, inPtr, BUFFERLEN - inLen); if (res > 0) { if (verbose & PKT_DEBUG_LOG) @@ -241,3 +244,25 @@ terminated() termLen = l; return false; } + +bool packet:: +linkFailed() +{ + int arg; + bool failed = false; + int res = ioctl(fd, TIOCMGET, &arg); + if (res < 0) + failed = true; + if (verbose & PKT_DEBUG_DUMP) + cout << "packet: DTR:" << ((arg & TIOCM_DTR)?1:0) + << " RTS:" << ((arg & TIOCM_RTS)?1:0) + << " DCD:" << ((arg & TIOCM_CAR)?1:0) + << " DSR:" << ((arg & TIOCM_DSR)?1:0) + << " CTS:" << ((arg & TIOCM_CTS)?1:0) << endl; + if (((arg & TIOCM_DSR) == 0) || ((arg & TIOCM_CTS) == 0)) + failed = true; + if ((verbose & PKT_DEBUG_LOG) && failed) + cout << "packet: linkFAILED\n"; + return failed; +} + diff --git a/ncpd/packet.h b/ncpd/packet.h index 85a0a77..8348ba7 100644 --- a/ncpd/packet.h +++ b/ncpd/packet.h @@ -19,6 +19,7 @@ class packet { bool get(unsigned char &type, bufferStore &b); void setVerbose(short int); short int getVerbose(); + bool linkFailed(); private: bool terminated(); -- cgit v1.2.3