From c71c35f1cbadab07cc64d80f8b785a32e0bfd9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Sun, 9 Aug 2020 17:03:59 +0200 Subject: [PATCH 1/2] Send SIGTERM to pid instead of SIGINT to group. --- src/main.cc | 3 ++- src/serial.cc | 6 ++++++ src/shell.cc | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main.cc b/src/main.cc index ff1be1f..6d194c8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -97,7 +97,9 @@ provided you with this binary. Read the full license for all details. // We want the data visible in the log file asap! setbuf(stdout, NULL); startUsingCommandline(cmdline.get()); + exit(0); } + error("(main) internal error\n"); } bool startUsingCommandline(Configuration *config) @@ -368,7 +370,6 @@ LIST_OF_METERS } manager->waitForStop(); - if (config->daemon) { notice("(wmbusmeters) shutting down\n"); } diff --git a/src/serial.cc b/src/serial.cc index d97e2e0..a60d194 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -878,6 +878,7 @@ static int openSerialTTY(const char *tty, int baud_rate) int rc = 0; speed_t speed = 0; struct termios tios; + //int DTR_flag = TIOCM_DTR; int fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { @@ -930,6 +931,11 @@ static int openSerialTTY(const char *tty, int baud_rate) rc = tcsetattr(fd, TCSANOW, &tios); if (rc < 0) goto err; + // This code can toggle DTR... maybe necessary + // for the pl2303 usb2serial driver/device. + //rc = ioctl(fd, TIOCMBIC, &DTR_flag); + //if (rc != 0) goto err; + return fd; err: diff --git a/src/shell.cc b/src/shell.cc index acc2ba5..6f46bd0 100644 --- a/src/shell.cc +++ b/src/shell.cc @@ -190,7 +190,7 @@ void stopBackgroundShell(int pid) // The solution for now is to send sigint to 0, which // means send sigint to the whole process group that the // sender belongs to. - int rc = kill(0, SIGINT); + int rc = kill(pid, SIGTERM); if (rc < 0) { debug("(bgshell) could not sigint pid %d, exited already?\n", pid); return; From a42e8c9a5f7a5540796d1a62cb0e70cff11bb9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Sun, 9 Aug 2020 17:05:55 +0200 Subject: [PATCH 2/2] Updated comment. --- src/shell.cc | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/shell.cc b/src/shell.cc index 6f46bd0..e3b31d8 100644 --- a/src/shell.cc +++ b/src/shell.cc @@ -179,17 +179,8 @@ void stopBackgroundShell(int pid) { assert(pid > 0); - // This will actually stop the entire process group. - // But it is ok, for now, since this function is - // only called when wmbusmeters is exiting. - - // If we send sigint only to pid, then this will - // not always propagate properly to the child processes - // of the bgshell, ie rtl_sdr and rtl_wmbus, thus - // leaving those hanging in limbo and messing everything up. - // The solution for now is to send sigint to 0, which - // means send sigint to the whole process group that the - // sender belongs to. + // Sending SIGTERM to the pid will properly shut down the subshell + // and its contents. int rc = kill(pid, SIGTERM); if (rc < 0) { debug("(bgshell) could not sigint pid %d, exited already?\n", pid);