From fc61ebaf53889ce29c3c24d9f7f95d6d599c1363 Mon Sep 17 00:00:00 2001 From: Alexander Chemeris Date: Wed, 24 Nov 2010 12:23:30 +0300 Subject: [PATCH] Correctly exit OpenBTS even if console is not started yet. --- public-trunk/apps/OpenBTS.cpp | 40 ++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/public-trunk/apps/OpenBTS.cpp b/public-trunk/apps/OpenBTS.cpp index 551341e..f742b1f 100644 --- a/public-trunk/apps/OpenBTS.cpp +++ b/public-trunk/apps/OpenBTS.cpp @@ -238,15 +238,38 @@ static int startTransceiver() return EXIT_SUCCESS; } +static void serverCleanup() +{ + if (sgTransceiverPid) { + kill(sgTransceiverPid, SIGTERM); + if (sgTransceiverPidFileFd >= 0) { + close(sgTransceiverPidFileFd); + } + if (sgTransceiverPidFile.size() > 0) { + if (unlink(sgTransceiverPidFile.data()) == 0) { + LOG(INFO) << "Deleted lock file " << sgTransceiverPidFile; + } else { + LOG(INFO) << "Error while deleting lock file " << sgTransceiverPidFile + << " code=" << errno << ": " << strerror(errno); + } + } + } +} + static void exitCLI() { - if (sgCLIServerSock != NULL) { + if (sgCLIServerSock == NULL) { + serverCleanup(); + _exit(EXIT_SUCCESS); + } else { // Closing server sock sgCLIServerSock->close(); sgCLIServerSock = NULL; } // Closing server standard input to shutdown local CLI + // Following functions are not async-signal-safe, but I don't have + // better idea how to do this. cin.setstate(ios::eofbit); // cin.putback('\n'); fclose(stdin); @@ -653,20 +676,7 @@ int main(int argc, char *argv[]) exitBTS(0, cout); } - if (sgTransceiverPid) { - kill(sgTransceiverPid, SIGTERM); - if (sgTransceiverPidFileFd >= 0) { - close(sgTransceiverPidFileFd); - } - if (sgTransceiverPidFile.size() > 0) { - if (unlink(sgTransceiverPidFile.data()) == 0) { - LOG(INFO) << "Deleted lock file " << sgTransceiverPidFile; - } else { - LOG(INFO) << "Error while deleting lock file " << sgTransceiverPidFile - << " code=" << errno << ": " << strerror(errno); - } - } - } + serverCleanup(); return EXIT_SUCCESS; }