From c4dd409aaca4924ee1292f3cf9d0f10094634278 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Wed, 8 Feb 2017 22:16:38 +0800 Subject: [PATCH] Fix signal string not work, revert to sys_signame --- src/protocol.c | 2 +- src/utils.c | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/protocol.c b/src/protocol.c index 382f4a7..658542e 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -84,7 +84,7 @@ tty_client_destroy(struct tty_client *client) { client->running = false; // kill process and free resource - lwsl_notice("sending %s to process %d\n", server->sig_name, client->pid); + lwsl_notice("sending %s (%d) to process %d\n", server->sig_name, server->sig_code, client->pid); if (kill(client->pid, server->sig_code) != 0) { lwsl_err("kill: pid, errno: %d (%s)\n", client->pid, errno, strerror(errno)); } diff --git a/src/utils.c b/src/utils.c index b0bef4c..b2fe8d6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,9 +7,29 @@ #include #include +#ifdef __linux__ +// https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c +const char *sys_signame[NSIG] = { + "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED", + "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", + "STKFLT","CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", + "XCPU", "XFSZ", "VTALRM","PROF", "WINCH", "IO", "PWR", "SYS", NULL +}; +#endif + #if defined(_WIN32) || defined(__CYGWIN__) #include #include +// https://github.com/mirror/newlib-cygwin/blob/master/winsup/cygwin/strsig.cc +#ifndef NSIG +#define NSIG 33 +#endif +const char *sys_signame[NSIG] = { + "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", + "FPE", "KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", + "URG", "STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", + "XCPU", "XFSZ", "VTALRM","PROF", "WINCH", "PWR", "USR1", "USR2", NULL +}; #endif void * @@ -50,19 +70,19 @@ endswith(const char *str, const char *suffix) { int get_sig_name(int sig, char *buf) { - int n = sprintf(buf, "SIG%s", sig < NSIG ? strsignal(sig) : "unknown"); + int n = sprintf(buf, "SIG%s", sig < NSIG ? sys_signame[sig] : "unknown"); uppercase(buf); return n; } int get_sig(const char *sig_name) { - if (strcasestr(sig_name, "sig") != sig_name || strlen(sig_name) <= 3) { + if (strlen(sig_name) <= 3 || strcasestr(sig_name, "sig") == NULL) { return -1; } for (int sig = 1; sig < NSIG; sig++) { - const char *name = strsignal(sig); - if (strcasecmp(name, sig_name + 3) == 0) + const char *name = sys_signame[sig]; + if (name != NULL && strcasecmp(name, sig_name + 3) == 0) return sig; } return -1; -- 2.43.4