From 4ec42fa0898ab268089b4493195463a3b7feec72 Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:04:10 +0900 Subject: [PATCH 01/10] candump: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. Signed-off-by: Yasushi SHOJI --- candump.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/candump.c b/candump.c index 82f75b1..d855020 100644 --- a/candump.c +++ b/candump.c @@ -118,6 +118,7 @@ static const char extra_m_info[4][4] = { "- -", "B -", "- E", "B E" }; extern int optind, opterr, optopt; static volatile int running = 1; +static volatile sig_atomic_t signal_num; static void print_usage(void) { @@ -169,6 +170,7 @@ static void print_usage(void) static void sigterm(int signo) { running = 0; + signal_num = signo; } static int idx2dindex(int ifidx, int socket) @@ -861,5 +863,8 @@ int main(int argc, char **argv) if (log) fclose(logfile); + if (signal_num) + return 128 + signal_num; + return 0; } From 74e0d28d1aaa3d8dab1a80f84ae0c5c5f2328406 Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:04:56 +0900 Subject: [PATCH 02/10] cangen: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. Signed-off-by: Yasushi SHOJI --- cangen.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cangen.c b/cangen.c index bd88d9f..688a73a 100644 --- a/cangen.c +++ b/cangen.c @@ -91,6 +91,7 @@ extern int optind, opterr, optopt; static volatile int running = 1; +static volatile sig_atomic_t signal_num; static unsigned long long enobufs_count; static bool ignore_enobufs; static bool use_so_txtime; @@ -220,6 +221,7 @@ static void print_usage(char *prg) static void sigterm(int signo) { running = 0; + signal_num = signo; } static int setsockopt_txtime(int fd) @@ -887,5 +889,8 @@ int main(int argc, char **argv) close(s); + if (signal_num) + return 128 + signal_num; + return 0; } From 3caf69292520f1c5ee706242ab54a8ce666f2e5a Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:05:19 +0900 Subject: [PATCH 03/10] cansequence: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. Signed-off-by: Yasushi SHOJI --- cansequence.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cansequence.c b/cansequence.c index 3ba6912..e9861eb 100644 --- a/cansequence.c +++ b/cansequence.c @@ -33,6 +33,7 @@ extern int optind, opterr, optopt; static int s = -1; static bool running = true; +static volatile sig_atomic_t signal_num; static bool infinite = true; static unsigned int drop_until_quit; static unsigned int drop_count; @@ -73,6 +74,7 @@ static void print_usage(char *prg) static void sig_handler(int signo) { running = false; + signal_num = signo; } @@ -367,5 +369,8 @@ int main(int argc, char **argv) else do_send(); + if (signal_num) + return 128 + signal_num; + exit(EXIT_SUCCESS); } From 3ef3d7273853532871c259834fb28885173a09be Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:05:41 +0900 Subject: [PATCH 04/10] cansniffer: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. Signed-off-by: Yasushi SHOJI --- cansniffer.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cansniffer.c b/cansniffer.c index 5aa5e7a..975166d 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -124,6 +124,7 @@ extern int optind, opterr, optopt; static int idx; static int running = 1; +static volatile sig_atomic_t signal_num; static int clearscreen = 1; static int print_eff; static int print_ascii = 1; @@ -243,6 +244,7 @@ void print_usage(char *prg) void sigterm(int signo) { running = 0; + signal_num = signo; } int main(int argc, char **argv) @@ -428,6 +430,10 @@ int main(int argc, char **argv) printf("%s", CSR_SHOW); /* show cursor */ close(s); + + if (signal_num) + return 128 + signal_num; + return ret; } From 180eceff4d791c64654beff6e46b06a5e008f6c5 Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:05:58 +0900 Subject: [PATCH 05/10] isotptun: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. Signed-off-by: Yasushi SHOJI --- isotptun.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/isotptun.c b/isotptun.c index ee473fd..2a39ea7 100644 --- a/isotptun.c +++ b/isotptun.c @@ -79,6 +79,7 @@ #define BUF_LEN (MAX_PDU_LENGTH + 1) static volatile int running = 1; +static volatile sig_atomic_t signal_num; static void fake_syslog(int priority, const char *format, ...) { @@ -130,6 +131,7 @@ void print_usage(char *prg) void sigterm(int signo) { running = 0; + signal_num = signo; } int main(int argc, char **argv) @@ -403,5 +405,9 @@ int main(int argc, char **argv) close(s); close(t); + + if (signal_num) + return 128 + signal_num; + return EXIT_SUCCESS; } From 16a717c81e2d30f83bfda2c6e785260f255f074f Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 09:58:29 +0900 Subject: [PATCH 06/10] canbusload: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. canbusload.c was using a non-safe function, exit(3), in the signal handler. This commit replaces it with the way other programs in can-utils are using; set running = 0 to exit from the while loop. Signed-off-by: Yasushi SHOJI --- canbusload.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/canbusload.c b/canbusload.c index 47b62fd..83a6c80 100644 --- a/canbusload.c +++ b/canbusload.c @@ -82,6 +82,8 @@ static struct { unsigned int recv_bits_dbitrate; } stat[MAXSOCK+1]; +static volatile int running = 1; +static volatile sig_atomic_t signal_num; static int max_devname_len; /* to prevent frazzled device name output */ static int max_bitrate_len; static int currmax; @@ -124,7 +126,8 @@ void print_usage(char *prg) void sigterm(int signo) { - exit(0); + running = 0; + signal_num = signo; } void printstats(int signo) @@ -382,7 +385,7 @@ int main(int argc, char **argv) if (redraw) printf("%s", CLR_SCREEN); - while (1) { + while (running) { FD_ZERO(&rdfs); for (i=0; i Date: Mon, 20 Mar 2023 10:06:42 +0900 Subject: [PATCH 07/10] canlogserver: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. canlogserver.c was using a non-safe function, exit(3), in the signal handler. This commit replaces it with the way other programs in can-utils are using; set running = 0 to exit from the while loop. Signed-off-by: Yasushi SHOJI --- canlogserver.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/canlogserver.c b/canlogserver.c index 51d548f..aa41347 100644 --- a/canlogserver.c +++ b/canlogserver.c @@ -84,6 +84,7 @@ static int max_devname_len; extern int optind, opterr, optopt; static volatile int running = 1; +static volatile sig_atomic_t signal_num; void print_usage(char *prg) { @@ -164,7 +165,8 @@ void childdied(int i) */ void shutdown_gra(int i) { - exit(0); + running = 0; + signal_num = i; } @@ -433,5 +435,9 @@ int main(int argc, char **argv) close(s[i]); close(accsocket); + + if (signal_num) + return 128 + signal_num; + return 0; } From 7e35c7e1de06509fff64b3710630a0f16fd50a04 Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:07:33 +0900 Subject: [PATCH 08/10] canfdtest: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. canfdtest.c was effectively raising the received signal again to run the default signal handler. This has been declined by the maintainer because it's over-engineering. This commit replaces it with the method proposed by the maintainer. Signed-off-by: Yasushi SHOJI --- canfdtest.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index a5050bc..d47727f 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -584,10 +584,8 @@ int main(int argc, char *argv[]) close(sockfd); - if (exit_sig) { - signal(exit_sig, SIG_DFL); - kill(getpid(), exit_sig); - } + if (exit_sig) + return 128 + exit_sig; return err; } From 7490986650909ff7fc33565a021fdfbc7ed54b91 Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:11:22 +0900 Subject: [PATCH 09/10] j1939acd: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. For j1939acd.c, it was using s.sig_term as a boolean key to exit from the while loop. This commit replaces a.sig_term with a.signal_num, which holds the signal it received. Signed-off-by: Yasushi SHOJI --- j1939acd.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/j1939acd.c b/j1939acd.c index a1d7162..44bb826 100644 --- a/j1939acd.c +++ b/j1939acd.c @@ -110,7 +110,7 @@ static struct { uint64_t name; uint8_t current_sa; uint8_t last_sa; - int sig_term; + volatile sig_atomic_t signal_num; int sig_alrm; int sig_usr1; int state; @@ -337,7 +337,7 @@ static void sighandler(int sig, siginfo_t *info, void *vp) switch (sig) { case SIGINT: case SIGTERM: - s.sig_term = 1; + s.signal_num = sig; break; case SIGALRM: s.sig_alrm = 1; @@ -531,7 +531,7 @@ int main(int argc, char *argv[]) install_signal(SIGUSR1); install_signal(SIGUSR2); - while (!s.sig_term) { + while (!s.signal_num) { if (s.sig_usr1) { s.sig_usr1 = 0; dump_status(); @@ -648,6 +648,9 @@ done: fprintf(stderr, "- shutdown\n"); claim_address(sock, s.name, J1939_IDLE_ADDR); save_cache(); + + if (s.signal_num) + return 128 + s.signal_num; + return 0; } - From 69ded0fbad90f7992465647b1448c992d3638a8f Mon Sep 17 00:00:00 2001 From: Yasushi SHOJI Date: Mon, 20 Mar 2023 10:14:03 +0900 Subject: [PATCH 10/10] slcand: Follow Bash exit status when signaled Bash and many other shells use 128 + signum as the exit status when a program get signal and exit. Follow the common behavior so that we know how the programs are killed. slcand was returning 0 when SIGINT and SIGTERM is received. This commit, instead, return 128 + signum when it gets a signal. Signed-off-by: Yasushi SHOJI --- slcand.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/slcand.c b/slcand.c index ac0c044..4eb4450 100644 --- a/slcand.c +++ b/slcand.c @@ -93,7 +93,7 @@ void print_usage(char *prg) } static int slcand_running; -static int exit_code; +static volatile sig_atomic_t exit_code; static char ttypath[TTYPATH_LENGTH]; static void child_handler(int signum) @@ -104,16 +104,12 @@ static void child_handler(int signum) /* exit parent */ exit(EXIT_SUCCESS); break; + case SIGINT: + case SIGTERM: case SIGALRM: case SIGCHLD: syslogger(LOG_NOTICE, "received signal %i on %s", signum, ttypath); - exit_code = EXIT_FAILURE; - slcand_running = 0; - break; - case SIGINT: - case SIGTERM: - syslogger(LOG_NOTICE, "received signal %i on %s", signum, ttypath); - exit_code = EXIT_SUCCESS; + exit_code = 128 + signum; slcand_running = 0; break; }