From 668256290d1c48319b0b96d41559efb48dcc0821 Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Tue, 9 Jul 2019 19:38:23 +0800 Subject: [PATCH] [spi-hdlc-adapter] add syslog to record signal (#3966) --- tools/spi-hdlc-adapter/spi-hdlc-adapter.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/spi-hdlc-adapter/spi-hdlc-adapter.c b/tools/spi-hdlc-adapter/spi-hdlc-adapter.c index e61efa295..bc94c8e91 100644 --- a/tools/spi-hdlc-adapter/spi-hdlc-adapter.c +++ b/tools/spi-hdlc-adapter/spi-hdlc-adapter.c @@ -171,6 +171,8 @@ static int sSpiSmallPacketSize = 32; // in bytes static bool sSlaveDidReset = false; +static int sCaughtSignal = -1; + // If sUseRawFrames is set to true, HDLC encoding/encoding // is skipped and the raw frames are read-from/written-to // the sHdlcInputFd/sHdlcOutputFd whole. See `--raw`. @@ -212,6 +214,7 @@ static void signal_SIGINT(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); + sCaughtSignal = sig; // Restore the previous handler so that if we end up getting // this signal again we perform the system default action. @@ -231,6 +234,7 @@ static void signal_SIGTERM(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); + sCaughtSignal = sig; // Restore the previous handler so that if we end up getting // this signal again we perform the system default action. @@ -250,6 +254,7 @@ static void signal_SIGHUP(int sig) // Can't use syslog() because it isn't async signal safe. // So we write to stderr IGNORE_RETURN_VALUE(write(STDERR_FILENO, message, sizeof(message) - 1)); + sCaughtSignal = sig; // We don't restore the "previous handler" // because we always want to let the main @@ -569,7 +574,8 @@ static int push_pull_spi(void) if (ret < 0) { - perror("do_spi_xfer"); + perror("push_pull_spi:do_spi_xfer"); + syslog(LOG_ERR, "push_pull_spi:do_spi_xfer: errno=%d (%s)", errno, strerror(errno)); // Print out a helpful error message for // a common error. @@ -1993,6 +1999,11 @@ int main(int argc, char *argv[]) // SHUTDOWN bail: + if (sCaughtSignal != -1) + { + syslog(LOG_ERR, "Caught %s", strsignal(sCaughtSignal)); + } + syslog(LOG_NOTICE, "Shutdown. (sRet = %d)", sRet); syslog(LOG_NOTICE, "Reset NCP/RCP");