From f2d9bc87bc87b4ef3cdee081c65ad1c248804edd Mon Sep 17 00:00:00 2001 From: Fritz Elfert Date: Sun, 4 Jul 1999 11:59:42 +0000 Subject: logging via syslog, more error-handling. --- ncpd/Makefile.am | 6 ++- ncpd/main.cc | 111 ++++++++++++++++++++++++++----------------------------- 2 files changed, 58 insertions(+), 59 deletions(-) (limited to 'ncpd') diff --git a/ncpd/Makefile.am b/ncpd/Makefile.am index 4c6ddb7..b966d2a 100644 --- a/ncpd/Makefile.am +++ b/ncpd/Makefile.am @@ -1,6 +1,10 @@ INCLUDES=-I../lib +HERE=$(shell pwd) sbin_PROGRAMS = ncpd -ncpd_LDADD = ../lib/.libs/libplp.so + +ncpd_LDADD = ../lib/libplp.la +#ncpd_LDADD = -L$(HERE)/../lib -L$(HERE)/../lib/.libs -lplp ncpd_SOURCES = channel.cc link.cc linkchan.cc main.cc \ ncp.cc packet.cc socketchan.cc mp_serial.c +EXTRA_DIST = channel.h link.h linkchan.h mp_serial.h ncp.h packet.h socketchan.h diff --git a/ncpd/main.cc b/ncpd/main.cc index fca9efc..1dc360f 100644 --- a/ncpd/main.cc +++ b/ncpd/main.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include "defs.h" #include "ncp.h" @@ -33,6 +34,9 @@ #include "linkchan.h" #include "link.h" #include "packet.h" +#include "log.h" + +static bool verbose = false; void checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp * a, IOWatch & iow) @@ -41,12 +45,12 @@ checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp ppsocket *next = skt.accept(peer, 200); if (next != NULL) { // New connect - cout << "New socket connection from " << peer << endl; + if (verbose) + cout << "New socket connection from " << peer << endl; if ((numScp == 7) || (!a->gotLinkChannel())) { bufferStore a; - a.addStringT("No psion ncp channel free"); + a.addStringT("No psion"); next->sendBufferStore(a); - sleep(1); next->closeSocket(); } else scp[numScp++] = new socketChan(next, a, iow); @@ -69,32 +73,10 @@ pollSocketConnections(int &numScp, socketChan ** scp) } } -void -resetSocketConnections(int &numScp, socketChan ** scp, ncp * a) -{ - for (int i = 0; i < numScp; i++) { - if (scp[i]->isConnected()) { - cout << "Killing\n"; - delete scp[i]; - numScp--; - for (int j = i; j < numScp; j++) - scp[j] = scp[j + 1]; - i--; - } else { - scp[i]->newNcpController(a); - if (scp[i]->getNcpConnectName() != NULL) { - cout << "Connecting\n"; - scp[i]->ncpConnect(); - } else - cout << "Ignoring\n"; - } - } -} - void usage() { - cerr << "Usage : ncpd [-s ] [-d ] [-b ]\n"; + cerr << "Usage : ncpd [-V] [-v logclass] [-p ] [-d ] [-b ]\n"; exit(1); } @@ -103,7 +85,7 @@ main(int argc, char **argv) { ppsocket skt; IOWatch iow; - skt.startup(); + int pid; // Command line parameter processing int sockNum = DPORT; @@ -113,8 +95,9 @@ main(int argc, char **argv) short int pverbose = 0; short int lverbose = 0; + signal(SIGPIPE, SIG_IGN); for (int i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-s") && i + 1 < argc) { + if (!strcmp(argv[i], "-p") && i + 1 < argc) { sockNum = atoi(argv[++i]); } else if (!strcmp(argv[i], "-d") && i + 1 < argc) { serialDevice = argv[++i]; @@ -132,52 +115,64 @@ main(int argc, char **argv) pverbose |= PKT_DEBUG_LOG; if (!strcmp(argv[i], "pd")) pverbose |= PKT_DEBUG_DUMP; + if (!strcmp(argv[i], "m")) + verbose = true; } else if (!strcmp(argv[i], "-b") && i + 1 < argc) { baudRate = atoi(argv[++i]); } else if (!strcmp(argv[i], "-V")) { - cout << "plpnfsd version " << VERSION << endl; + cout << "ncpd version " << VERSION << endl; exit(0); } else usage(); } - if (!skt.listen("127.0.0.1", sockNum)) { - cerr << "Could not initiate listen on socket " << sockNum << endl; - } else { - ncp *a = NULL; - int numScp; - socketChan *scp[8]; + switch ((pid = fork())) { + 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); + ncp *a = new ncp(serialDevice, baudRate, iow); + int numScp = 0; + socketChan *scp[8]; - while (true) { - if (a == NULL) { - a = new ncp(serialDevice, baudRate, iow); a->setVerbose(nverbose); a->setLinkVerbose(lverbose); a->setPktVerbose(pverbose); - numScp = 0; iow.addIO(skt.socket()); - } - // sockets - pollSocketConnections(numScp, scp); - checkForNewSocketConnection(skt, numScp, scp, a, iow); + while (true) { + // sockets + pollSocketConnections(numScp, scp); + checkForNewSocketConnection(skt, numScp, scp, a, iow); - // psion - a->poll(); + // psion + a->poll(); - if (a->stuffToSend()) - iow.watch(0, 100000); - else - iow.watch(100000, 0); + if (a->stuffToSend()) + iow.watch(0, 100000); + else + iow.watch(100000, 0); - if (a->hasFailed()) { - cout << "ncp: restarting\n"; - // resetSocketConnections(numScp, scp, a); - // delete a; - // a = NULL; - a->reset(); + if (a->hasFailed()) { + if (verbose) + cout << "ncp: restarting\n"; + a->reset(); + } + } + delete a; } - } - delete a; + skt.closeSocket(); + break; + case -1: + cerr << "fork: " << strerror(errno) << endl; + break; + default: + exit(0); } - skt.closeSocket(); } -- cgit v1.2.3