Support numeric value for --signal

This commit is contained in:
Shuanglei Tao
2017-11-05 11:17:03 +08:00
parent a59da79d62
commit a5b8905c19
6 changed files with 17 additions and 22 deletions

View File

@@ -50,7 +50,7 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
.PP
\-s, \-\-signal <signal string>
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 <seconds>

View File

@@ -34,7 +34,7 @@ ttyd 1 "September 2016" ttyd "User Manual"
Group id to run with
-s, --signal <signal string>
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 <seconds>
Time to reconnect for the client in seconds (default: 10)

View File

@@ -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;

View File

@@ -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;
};

View File

@@ -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

View File

@@ -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