From: Shuanglei Tao Date: Sun, 5 Nov 2017 03:17:03 +0000 (+0800) Subject: Support numeric value for --signal X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=a5b8905c19f497a76e321486d9bd30cfb633a9f2;p=ttyd.git Support numeric value for --signal --- diff --git a/man/ttyd.1 b/man/ttyd.1 index 3611222..79512ca 100644 --- a/man/ttyd.1 +++ b/man/ttyd.1 @@ -50,7 +50,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows .PP \-s, \-\-signal - Signal to send to the command when exit it (default: SIGHUP) + Signal to send to the command when exit it (default: 9, SIGHUP) .PP \-r, \-\-reconnect diff --git a/man/ttyd.man.md b/man/ttyd.man.md index bae30f8..d51fbed 100644 --- a/man/ttyd.man.md +++ b/man/ttyd.man.md @@ -34,7 +34,7 @@ ttyd 1 "September 2016" ttyd "User Manual" Group id to run with -s, --signal - Signal to send to the command when exit it (default: SIGHUP) + Signal to send to the command when exit it (default: 9, SIGHUP) -r, --reconnect Time to reconnect for the client in seconds (default: 10) diff --git a/src/server.c b/src/server.c index ddb2db5..3d1cf51 100644 --- a/src/server.c +++ b/src/server.c @@ -57,7 +57,7 @@ void print_help() { " -c, --credential Credential for Basic Authentication (format: username:password)\n" " -u, --uid User id to run with\n" " -g, --gid Group id to run with\n" - " -s, --signal Signal to send to the command when exit it (default: SIGHUP)\n" + " -s, --signal Signal to send to the command when exit it (default: 9, SIGHUP)\n" " -r, --reconnect Time to reconnect for the client in seconds (default: 10)\n" " -R, --readonly Do not allow clients to write to the TTY\n" " -t, --client-option Send option to client (format: key=value), repeat to add more options\n" @@ -90,7 +90,7 @@ tty_server_new(int argc, char **argv, int start) { ts->client_count = 0; ts->reconnect = 10; ts->sig_code = SIGHUP; - ts->sig_name = strdup("SIGHUP"); + get_sig_name(ts->sig_code, ts->sig_name, sizeof(ts->sig_name)); if (start == argc) return ts; @@ -134,13 +134,11 @@ tty_server_free(struct tty_server *ts) { free(ts->argv[i++]); } while (ts->argv[i] != NULL); free(ts->argv); - free(ts->sig_name); - if (ts->socket_path != NULL) { + if (strlen(ts->socket_path) > 0) { struct stat st; if (!stat(ts->socket_path, &st)) { unlink(ts->socket_path); } - free(ts->socket_path); } free(ts); } @@ -151,7 +149,7 @@ sig_handler(int sig) { exit(EXIT_FAILURE); char sig_name[20]; - get_sig_name(sig, sig_name); + get_sig_name(sig, sig_name, sizeof(sig_name)); lwsl_notice("received signal: %s (%d), exiting...\n", sig_name, sig); force_exit = true; lws_cancel_service(context); @@ -285,8 +283,8 @@ main(int argc, char **argv) { case 's': { int sig = get_sig(optarg); if (sig > 0) { - server->sig_code = get_sig(optarg); - server->sig_name = uppercase(strdup(optarg)); + server->sig_code = sig; + get_sig_name(sig, server->sig_name, sizeof(server->sig_code)); } else { fprintf(stderr, "ttyd: invalid signal: %s\n", optarg); return -1; @@ -374,9 +372,9 @@ main(int argc, char **argv) { if (strlen(iface) > 0) { info.iface = iface; if (endswith(info.iface, ".sock") || endswith(info.iface, ".socket")) { -#ifdef LWS_USE_UNIX_SOCK +#if defined(LWS_USE_UNIX_SOCK) || defined(LWS_WITH_UNIX_SOCK) info.options |= LWS_SERVER_OPTION_UNIX_SOCK; - server->socket_path = strdup(info.iface); + strncpy(server->socket_path, info.iface, sizeof(server->socket_path)); #else fprintf(stderr, "libwebsockets is not compiled with UNIX domain socket support"); return -1; diff --git a/src/server.h b/src/server.h index 93dac07..02bd190 100644 --- a/src/server.h +++ b/src/server.h @@ -107,12 +107,12 @@ struct tty_server { char *command; // full command line char **argv; // command with arguments int sig_code; // close signal - char *sig_name; // human readable signal string + char sig_name[20]; // human readable signal string bool readonly; // whether not allow clients to write to the TTY bool check_origin; // whether allow websocket connection from different origin int max_clients; // maximum clients to support bool once; // whether accept only one client and exit on disconnection - char *socket_path; // UNIX domain socket path + char socket_path[255]; // UNIX domain socket path pthread_mutex_t lock; }; diff --git a/src/utils.c b/src/utils.c index b2fe8d6..ac16bd1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -69,23 +69,20 @@ endswith(const char *str, const char *suffix) { } int -get_sig_name(int sig, char *buf) { - int n = sprintf(buf, "SIG%s", sig < NSIG ? sys_signame[sig] : "unknown"); +get_sig_name(int sig, char *buf, size_t len) { + int n = snprintf(buf, len, "SIG%s", sig < NSIG ? sys_signame[sig] : "unknown"); uppercase(buf); return n; } int get_sig(const char *sig_name) { - if (strlen(sig_name) <= 3 || strcasestr(sig_name, "sig") == NULL) { - return -1; - } for (int sig = 1; sig < NSIG; sig++) { const char *name = sys_signame[sig]; - if (name != NULL && strcasecmp(name, sig_name + 3) == 0) + if (name != NULL && (strcasecmp(name, sig_name) == 0 || strcasecmp(name, sig_name + 3) == 0)) return sig; } - return -1; + return atoi(sig_name); } int diff --git a/src/utils.h b/src/utils.h index 7ac0008..1781c6a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,7 +19,7 @@ endswith(const char *str, const char *suffix); // Get human readable signal string int -get_sig_name(int sig, char *buf); +get_sig_name(int sig, char *buf, size_t len); // Get signal code from string like SIGHUP int