diff --git a/src/njs_builtin.c b/src/njs_builtin.c index 78c24ac71..b5e683645 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -100,6 +100,36 @@ static const njs_object_type_init_t *const }; +typedef struct { + njs_str_t name; + int value; +} njs_signal_entry_t; + +// P1990 signals from `man 7 signal` are supported +static njs_signal_entry_t njs_signals_table[] = { + { njs_str("SIGABRT"), SIGABRT }, + { njs_str("SIGALRM"), SIGALRM }, + { njs_str("SIGCHLD"), SIGCHLD }, + { njs_str("SIGCONT"), SIGCONT }, + { njs_str("SIGFPE"), SIGFPE }, + { njs_str("SIGHUP"), SIGHUP }, + { njs_str("SIGILL"), SIGILL }, + { njs_str("SIGINT"), SIGINT }, + { njs_str("SIGKILL"), SIGKILL }, + { njs_str("SIGPIPE"), SIGPIPE }, + { njs_str("SIGQUIT"), SIGQUIT }, + { njs_str("SIGSEGV"), SIGSEGV }, + { njs_str("SIGSTOP"), SIGSTOP }, + { njs_str("SIGTSTP"), SIGTSTP }, + { njs_str("SIGTERM"), SIGTERM }, + { njs_str("SIGTTIN"), SIGTTIN }, + { njs_str("SIGTTOU"), SIGTTOU }, + { njs_str("SIGUSR1"), SIGUSR1 }, + { njs_str("SIGUSR2"), SIGUSR2 }, + { njs_null_str, 0 } +}; + + njs_inline njs_int_t njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, const njs_object_init_t *init) @@ -1399,148 +1429,18 @@ njs_ext_process_kill(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, if (njs_value_is_number(arg)) { signal = njs_value_number(arg); } else if (njs_value_is_string(arg)) { + njs_signal_entry_t *s; njs_str_t str; njs_value_string_get(arg, &str); - if (njs_strncmp(str.start, "SIGTERM", njs_length("SIGTERM")) == 0) { - signal = SIGTERM; - } - #ifdef SIGABRT - else if (njs_strncmp(str.start, "SIGABRT", njs_length("SIGABRT")) == 0) { - signal = SIGABRT; - } - #endif - #ifdef SIGALRM - else if (njs_strncmp(str.start, "SIGALRM", njs_length("SIGALRM")) == 0) { - signal = SIGALRM; - } - #endif - #ifdef SIGBUS - else if (njs_strncmp(str.start, "SIGBUS", njs_length("SIGBUS")) == 0) { - signal = SIGBUS; - } - #endif - #ifdef SIGCHLD - else if (njs_strncmp(str.start, "SIGCHLD", njs_length("SIGCHLD")) == 0) { - signal = SIGCHLD; - } - #endif - #ifdef SIGCONT - else if (njs_strncmp(str.start, "SIGCONT", njs_length("SIGCONT")) == 0) { - signal = SIGCONT; - } - #endif - #ifdef SIGFPE - else if (njs_strncmp(str.start, "SIGFPE", njs_length("SIGFPE")) == 0) { - signal = SIGFPE; - } - #endif - #ifdef SIGHUP - else if (njs_strncmp(str.start, "SIGHUP", njs_length("SIGHUP")) == 0) { - signal = SIGHUP; - } - #endif - #ifdef SIGILL - else if (njs_strncmp(str.start, "SIGILL", njs_length("SIGILL")) == 0) { - signal = SIGILL; - } - #endif - #ifdef SIGINT - else if (njs_strncmp(str.start, "SIGINT", njs_length("SIGINT")) == 0) { - signal = SIGINT; - } - #endif - #ifdef SIGKILL - else if (njs_strncmp(str.start, "SIGKILL", njs_length("SIGKILL")) == 0) { - signal = SIGKILL; - } - #endif - #ifdef SIGPIPE - else if (njs_strncmp(str.start, "SIGPIPE", njs_length("SIGPIPE")) == 0) { - signal = SIGPIPE; - } - #endif - #ifdef SIGQUIT - else if (njs_strncmp(str.start, "SIGQUIT", njs_length("SIGQUIT")) == 0) { - signal = SIGQUIT; - } - #endif - #ifdef SIGSEGV - else if (njs_strncmp(str.start, "SIGSEGV", njs_length("SIGSEGV")) == 0) { - signal = SIGSEGV; - } - #endif - #ifdef SIGSTOP - else if (njs_strncmp(str.start, "SIGSTOP", njs_length("SIGSTOP")) == 0) { - signal = SIGSTOP; - } - #endif - #ifdef SIGTSTP - else if (njs_strncmp(str.start, "SIGTSTP", njs_length("SIGTSTP")) == 0) { - signal = SIGTSTP; - } - #endif - #ifdef SIGTTIN - else if (njs_strncmp(str.start, "SIGTTIN", njs_length("SIGTTIN")) == 0) { - signal = SIGTTIN; - } - #endif - #ifdef SIGTTOU - else if (njs_strncmp(str.start, "SIGTTOU", njs_length("SIGTTOU")) == 0) { - signal = SIGTTOU; - } - #endif - #ifdef SIGUSR1 - else if (njs_strncmp(str.start, "SIGUSR1", njs_length("SIGUSR1")) == 0) { - signal = SIGUSR1; - } - #endif - #ifdef SIGUSR2 - else if (njs_strncmp(str.start, "SIGUSR2", njs_length("SIGUSR2")) == 0) { - signal = SIGUSR2; - } - #endif - #ifdef SIGPOLL - else if (njs_strncmp(str.start, "SIGPOLL", njs_length("SIGPOLL")) == 0) { - signal = SIGPOLL; - } - #endif - #ifdef SIGPROF - else if (njs_strncmp(str.start, "SIGPROF", njs_length("SIGPROF")) == 0) { - signal = SIGPROF; - } - #endif - #ifdef SIGSYS - else if (njs_strncmp(str.start, "SIGSYS", njs_length("SIGSYS")) == 0) { - signal = SIGSYS; - } - #endif - #ifdef SIGTRAP - else if (njs_strncmp(str.start, "SIGTRAP", njs_length("SIGTRAP")) == 0) { - signal = SIGTRAP; - } - #endif - #ifdef SIGURG - else if (njs_strncmp(str.start, "SIGURG", njs_length("SIGURG")) == 0) { - signal = SIGURG; - } - #endif - #ifdef SIGVTALRM - else if (njs_strncmp(str.start, "SIGVTALRM", njs_length("SIGVTALRM")) == 0) { - signal = SIGVTALRM; - } - #endif - #ifdef SIGXCPU - else if (njs_strncmp(str.start, "SIGXCPU", njs_length("SIGXCPU")) == 0) { - signal = SIGXCPU; - } - #endif - #ifdef SIGXFSZ - else if (njs_strncmp(str.start, "SIGXFSZ", njs_length("SIGXFSZ")) == 0) { - signal = SIGXFSZ; + for (s = &njs_signals_table[0]; s->name.length != 0; s++) { + if (njs_strstr_eq(&str, &s->name)) { + signal = s->value; + break; + } } - #endif - else { + + if (s->name.length == 0) { njs_vm_type_error(vm, "\"signal\" unknown value"); return NJS_ERROR; }