From f6d25b8861f97439d7086e4569e209c46486d009 Mon Sep 17 00:00:00 2001 From: Fritz Elfert Date: Sun, 14 Jan 2001 22:28:37 +0000 Subject: - Added Hint for man pages in README. - Added signal handles for SIGINT and SIGTERM in ncpd for - properly shutting down the link. - Added flush() method in link.cc, needed for above feature. - If started with option -e and no -s option is given, assume being started by mgetty and use the line ncpd is started on instead of builtin default. - Added auto-reset in packet.cc. This helps making IrDA (ircomm-tty) based connections. Still not satisfying as i sometimes have to toggle infrared on the Psion (probably a bug in the Psion's IrDA). --- ncpd/link.cc | 9 +++++++- ncpd/link.h | 1 + ncpd/main.cc | 40 +++++++++++++++++++++++++++++---- ncpd/ncp.cc | 15 +++++++++++++ ncpd/packet.cc | 71 +++++++++++++++++++++++++++++++++++++++++++++------------- ncpd/packet.h | 2 ++ 6 files changed, 118 insertions(+), 20 deletions(-) (limited to 'ncpd') diff --git a/ncpd/link.cc b/ncpd/link.cc index fac136b..f4f73e4 100644 --- a/ncpd/link.cc +++ b/ncpd/link.cc @@ -50,6 +50,7 @@ link::link(const char *fname, int baud, IOWatch & iow, unsigned short _verbose) link::~link() { + flush(); delete p; } @@ -61,7 +62,7 @@ reset() { somethingToSend = false; timesSent = 0; failed = false; -// p->reset(); + // p->reset(); } short int link:: @@ -216,6 +217,12 @@ poll() return ret; } +void link:: +flush() { + while ((!failed) && stuffToSend()) + poll(); +} + bool link:: stuffToSend() { diff --git a/ncpd/link.h b/ncpd/link.h index 9c65cd8..23e2ac5 100644 --- a/ncpd/link.h +++ b/ncpd/link.h @@ -44,6 +44,7 @@ class link { bool stuffToSend(); bool hasFailed(); void reset(); + void flush(); void setVerbose(short int); short int getVerbose(); void setPktVerbose(short int); diff --git a/ncpd/main.cc b/ncpd/main.cc index a23267f..2392d09 100644 --- a/ncpd/main.cc +++ b/ncpd/main.cc @@ -43,6 +43,23 @@ #include "log.h" static bool verbose = false; +static bool active = true; + +static RETSIGTYPE +term_handler(int) +{ + cout << "Got SIGTERM" << endl; + signal(SIGTERM, term_handler); + active = false; +}; + +static RETSIGTYPE +int_handler(int) +{ + cout << "Got SIGINT" << endl; + signal(SIGINT, int_handler); + active = false; +}; void checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp * a, IOWatch & iow) @@ -97,7 +114,7 @@ main(int argc, char **argv) int sockNum = DPORT; int baudRate = DSPEED; - const char *serialDevice = DDEV; + const char *serialDevice = NULL; short int nverbose = 0; short int pverbose = 0; short int lverbose = 0; @@ -135,7 +152,8 @@ main(int argc, char **argv) if (!strcmp(argv[i], "all")) { nverbose = NCP_DEBUG_LOG | NCP_DEBUG_DUMP; lverbose = LNK_DEBUG_LOG | LNK_DEBUG_DUMP; - pverbose = PKT_DEBUG_LOG | PKT_DEBUG_DUMP; + pverbose = PKT_DEBUG_LOG | PKT_DEBUG_DUMP | + PKT_DEBUG_HANDSHAKE; verbose = true; } } else if (!strcmp(argv[i], "-b") && i + 1 < argc) { @@ -151,16 +169,27 @@ main(int argc, char **argv) usage(); } + if (serialDevice == NULL) { + // If started with -e, assume being started from mgetty and + // use the tty opened by mgetty instead of the builtin default. + if (autoexit) + serialDevice = ttyname(0); + else + serialDevice = DDEV; + } + if (dofork) pid = fork(); else pid = 0; switch (pid) { case 0: + signal(SIGTERM, term_handler); + signal(SIGINT, int_handler); if (!skt.listen("127.0.0.1", sockNum)) cerr << "listen on port " << sockNum << ": " << strerror(errno) << endl; else { - if (dofork) { + if (dofork || autoexit) { logbuf dlog(LOG_DEBUG); logbuf elog(LOG_ERR); ostream lout(&dlog); @@ -168,6 +197,9 @@ main(int argc, char **argv) cout = lout; cerr = lerr; openlog("ncpd", LOG_CONS|LOG_PID, LOG_DAEMON); + syslog(LOG_INFO, + "daemon started. Listening at 127.0.0.1:%d, using device %s\n", + sockNum, serialDevice); } ncp *a = new ncp(serialDevice, baudRate, iow); int numScp = 0; @@ -177,7 +209,7 @@ main(int argc, char **argv) a->setLinkVerbose(lverbose); a->setPktVerbose(pverbose); iow.addIO(skt.socket()); - while (true) { + while (active) { // sockets pollSocketConnections(numScp, scp); checkForNewSocketConnection(skt, numScp, scp, a, iow); diff --git a/ncpd/ncp.cc b/ncpd/ncp.cc index 3370cd1..1fbd533 100644 --- a/ncpd/ncp.cc +++ b/ncpd/ncp.cc @@ -53,6 +53,16 @@ ncp::ncp(const char *fname, int baud, IOWatch & iow) ncp::~ncp() { + bufferStore b; + for (int i = 0; i < MAX_CHANNEL; i++) { + if (channelPtr[i]) { + bufferStore b2; + b2.addByte(remoteChanList[i]); + controlChannel(i, NCON_MSG_CHANNEL_DISCONNECT, b2); + } + channelPtr[i] = NULL; + } + controlChannel(0, NCON_MSG_NCP_END, b); delete l; } @@ -65,6 +75,7 @@ reset() { } failed = false; lChan = NULL; + protocolVersion = PV_SERIES_5; // until detected on receipt of INFO l->reset(); } @@ -363,6 +374,10 @@ send(int channel, bufferStore & a) void ncp:: disconnect(int channel) { + if (channelPtr[channel] == NULL) { + cerr << "ncp: Ignored disconnect for unknown channel #" << channel << endl; + return; + } channelPtr[channel]->terminateWhenAsked(); if (verbose & NCP_DEBUG_LOG) cout << "ncp: disconnect: channel=" << channel << endl; diff --git a/ncpd/packet.cc b/ncpd/packet.cc index 2aa0509..5c05849 100644 --- a/ncpd/packet.cc +++ b/ncpd/packet.cc @@ -56,24 +56,42 @@ iow(_iow) inLen = outLen = termLen = 0; foundSync = 0; esc = false; + lastFatal = false; + serialStatus = -1; crcIn = crcOut = 0; fd = init_serial(devname, baud, 0); - iow.addIO(fd); + if (fd == -1) + lastFatal = true; + else + iow.addIO(fd); } void packet::reset() { - iow.remIO(fd); - ser_exit(fd); + if (verbose & PKT_DEBUG_LOG) + cout << "resetting serial connection" << endl; + if (fd != -1) { + iow.remIO(fd); + ser_exit(fd); + } usleep(100000); inLen = outLen = termLen = 0; + inPtr = inBuffer; + outPtr = outBuffer; foundSync = 0; + serialStatus = -1; esc = false; crcIn = crcOut = 0; fd = init_serial(devname, baud, 0); - iow.addIO(fd); + if (fd != -1) { + iow.addIO(fd); + lastFatal = false; + } + if (verbose & PKT_DEBUG_LOG) + cout << "serial connection reset, fd=" << fd << endl; + sleep(1); } short int packet:: @@ -90,8 +108,10 @@ setVerbose(short int _verbose) packet::~packet() { - iow.remIO(fd); - ser_exit(fd); + if (fd != -1) { + iow.remIO(fd); + ser_exit(fd); + } usleep(100000); delete[]inBuffer; delete[]outBuffer; @@ -287,24 +307,45 @@ bool packet:: linkFailed() { int arg; + int res; bool failed = false; - int res = ioctl(fd, TIOCMGET, &arg); + + if (lastFatal) + reset(); + res = ioctl(fd, TIOCMGET, &arg); if (res < 0) - failed = true; - if (verbose & PKT_DEBUG_HANDSHAKE) - 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; + lastFatal = true; + if ((serialStatus == -1) || (arg != serialStatus)) { + if (verbose & PKT_DEBUG_HANDSHAKE) + 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_RTS) && (arg & TIOCM_DTR))) { + arg |= (TIOCM_DTR | TIOCM_RTS); + res = ioctl(fd, TIOCMSET, &arg); + if (res < 0) + lastFatal = true; + if (verbose & PKT_DEBUG_HANDSHAKE) + 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; + } + serialStatus = arg; + } #ifdef sun if ((arg & TIOCM_CTS) == 0) #else if (((arg & TIOCM_DSR) == 0) || ((arg & TIOCM_CTS) == 0)) #endif failed = true; + if ((verbose & PKT_DEBUG_LOG) && lastFatal) + cout << "packet: linkFATAL\n"; if ((verbose & PKT_DEBUG_LOG) && failed) cout << "packet: linkFAILED\n"; - return failed; + return lastFatal || failed; } diff --git a/ncpd/packet.h b/ncpd/packet.h index 19385d3..86db078 100644 --- a/ncpd/packet.h +++ b/ncpd/packet.h @@ -66,8 +66,10 @@ class packet { int termLen; int foundSync; int fd; + int serialStatus; short int verbose; bool esc; + bool lastFatal; char *devname; int baud; IOWatch &iow; -- cgit v1.2.3