Skip to content

Commit

Permalink
Added process.kill() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Oct 28, 2024
1 parent 606ceb2 commit 9142f8a
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
183 changes: 183 additions & 0 deletions src/njs_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#include <njs_main.h>
#include <signal.h>


typedef struct {
Expand Down Expand Up @@ -1380,6 +1381,186 @@ njs_process_object_ppid(njs_vm_t *vm, njs_object_prop_t *prop,
}


static njs_int_t
njs_ext_process_kill(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t magic, njs_value_t *retval)
{
njs_uint_t pid, signal = SIGTERM;
njs_value_t *arg;

arg = njs_arg(args, nargs, 1);
if (!njs_value_is_number(arg)) {
njs_vm_type_error(vm, "\"pid\" is not a number");
return NJS_ERROR;
}
pid = njs_value_number(arg);

arg = njs_arg(args, nargs, 2);
if (njs_value_is_number(arg)) {
signal = njs_value_number(arg);
} else if (njs_value_is_string(arg)) {
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;
}
#endif
else {
njs_vm_type_error(vm, "\"signal\" unknown value");
return NJS_ERROR;
}
} else if (!njs_value_is_undefined(arg)) {
njs_vm_type_error(vm, "\"signal\" invalid type");
return NJS_ERROR;
}

int result = kill(pid, signal);

if (result == 0) {
njs_set_boolean(retval, 1);
return NJS_OK;
} else {
njs_vm_error(vm, "kill: %s", strerror(errno));
return NJS_ERROR;
}
}


static const njs_object_prop_t njs_process_object_properties[] =
{
{
Expand All @@ -1396,6 +1577,8 @@ static const njs_object_prop_t njs_process_object_properties[] =
NJS_DECLARE_PROP_HANDLER("pid", njs_process_object_pid, 0, 0, 0),

NJS_DECLARE_PROP_HANDLER("ppid", njs_process_object_ppid, 0, 0, 0),

NJS_DECLARE_PROP_NATIVE("kill", njs_ext_process_kill, 2, 0),
};


Expand Down
2 changes: 2 additions & 0 deletions test/shell_test.exp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ njs_run {"-c" "console.log(process.pid)"} "\\d+"

njs_run {"-c" "console.log(process.ppid)"} "\\d+"

njs_run {"-c" "console.log(process.kill(process.pid, 0))"} "true"


# script args

Expand Down
2 changes: 2 additions & 0 deletions test/shell_test_njs.exp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ njs_run {"-c" "console.log(process.pid)"} "\\d+"

njs_run {"-c" "console.log(process.ppid)"} "\\d+"

njs_run {"-c" "console.log(process.kill(process.pid, 0))"} "true"


# script args

Expand Down
5 changes: 5 additions & 0 deletions ts/njs_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ interface NjsProcess {
readonly ppid: number;
readonly argv: string[];
readonly env: NjsEnv;

/**
* @since 0.8.8
*/
kill(pid: number, signal?: string | number): true;
}

declare const process: NjsProcess;
Expand Down

0 comments on commit 9142f8a

Please sign in to comment.