From e0d2e82a9da1d2a22e06aa4c805226d8e2853c48 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:14:24 -0700 Subject: [PATCH 01/12] initial impl of Process.kill Signed-off-by: Brian Downs --- docs/docs/standard-lib/process.md | 14 ++++ src/cli/main.c | 20 +++--- src/optionals/process.c | 106 ++++++++++++++++++++++++++++-- 3 files changed, 125 insertions(+), 15 deletions(-) diff --git a/docs/docs/standard-lib/process.md b/docs/docs/standard-lib/process.md index 378343fb..3694c933 100644 --- a/docs/docs/standard-lib/process.md +++ b/docs/docs/standard-lib/process.md @@ -50,3 +50,17 @@ If the external process writes to stdout and you wish to capture the output you Process.run(["ls", "-la"]).unwrap(); print(Process.run(["echo", "test"], true).unwrap()); // 'test' ``` + +### Process.kill(Number, Number -> Optional) -> Result\ + +kill receives a process ID number and an optional signal number and attempts to kill the process associated with the given pid. If no signal is provided, SIGKILL is used. + +```cs +const res = Process.kill(709871); +// 0 +``` + +```cs +const res = Process.kill(709871, Process.SIGTERM).unwrap(); +// 0 +``` diff --git a/src/cli/main.c b/src/cli/main.c index 8bdaf95d..e9e1fe17 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -13,8 +13,7 @@ #include "linenoise/linenoise.h" -static int matchStringLiteral(char* line, int i) -{ +static int matchStringLiteral(char* line, int i) { char quote = line[i]; if (quote != '\'' && quote != '"') { @@ -62,8 +61,7 @@ static bool matchBraces(char *line) { return braceLevel == 0; } -static void memcpyAndAppendNul(char* dst, char* src, int len) -{ +static void memcpyAndAppendNul(char* dst, char* src, int len) { memcpy(dst, src, len); dst[len] = '\0'; } @@ -157,9 +155,9 @@ static void runFile(DictuVM *vm, char *filename) { } static const char *const usage[] = { - "dictu [options] [[--] args]", - "dictu [options]", - NULL, + "dictu [options] [[--] args]", + "dictu [options]", + NULL, }; int main(int argc, char *argv[]) { @@ -167,10 +165,10 @@ int main(int argc, char *argv[]) { char *cmd = NULL; struct argparse_option options[] = { - OPT_HELP(), - OPT_BOOLEAN('v', "version", &version, "Display Dictu version"), - OPT_STRING('c', "cmd", &cmd, "Run program passed in as string"), - OPT_END(), + OPT_HELP(), + OPT_BOOLEAN('v', "version", &version, "Display Dictu version"), + OPT_STRING('c', "cmd", &cmd, "Run program passed in as string"), + OPT_END(), }; struct argparse argparse; diff --git a/src/optionals/process.c b/src/optionals/process.c index 85284603..aff8c19d 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -1,3 +1,5 @@ +#include + #include "process.h" #ifdef _WIN32 @@ -222,7 +224,7 @@ static Value executeReturnOutput(DictuVM* vm, ObjList* argList) { } #endif -static Value execNative(DictuVM* vm, int argCount, Value* args) { +static Value execProcess(DictuVM* vm, int argCount, Value* args) { if (argCount != 1) { runtimeError(vm, "exec() takes 1 argument (%d given).", argCount); return EMPTY_VAL; @@ -237,7 +239,7 @@ static Value execNative(DictuVM* vm, int argCount, Value* args) { return execute(vm, argList, false); } -static Value runNative(DictuVM* vm, int argCount, Value* args) { +static Value runProcess(DictuVM* vm, int argCount, Value* args) { if (argCount != 1 && argCount != 2) { runtimeError(vm, "run() takes 1 or 2 arguments (%d given)", argCount); return EMPTY_VAL; @@ -268,6 +270,34 @@ static Value runNative(DictuVM* vm, int argCount, Value* args) { return execute(vm, argList, true); } +static Value killProcess(DictuVM* vm, int argCount, Value* args) { + if (argCount > 2) { + runtimeError(vm, "kill() takes 1 or 2 arguments (%d given)", argCount); + return EMPTY_VAL; + } + + if (!IS_NUMBER(args[0])) { + runtimeError(vm, "First argument passed to kill() must be a number"); + return EMPTY_VAL; + } + + pid_t pid = (pid_t)AS_NUMBER(args[0]); + int signal = SIGKILL; + + if (argCount == 2) { + if (!IS_NUMBER(args[1])) { + runtimeError(vm, "Second argument passed to kill() must be a number"); + return EMPTY_VAL; + } + + signal = AS_NUMBER(args[1]); + } + + int res = kill(pid, signal); + + return newResultSuccess(vm, AS_NUMBER(res)); +} + Value createProcessModule(DictuVM* vm) { ObjString* name = copyString(vm, "Process", 7); push(vm, OBJ_VAL(name)); @@ -277,12 +307,80 @@ Value createProcessModule(DictuVM* vm) { /** * Define process methods */ - defineNative(vm, &module->values, "exec", execNative); - defineNative(vm, &module->values, "run", runNative); + defineNative(vm, &module->values, "exec", execProcess); + defineNative(vm, &module->values, "run", runProcess); + defineNative(vm, &module->values, "kill", killProcess); /** * Define process properties */ + defineNativeProperty(vm, &module->values, "SIGINT", NUMBER_VAL(SIGINT)); + defineNativeProperty(vm, &module->values, "SIGILL", NUMBER_VAL(SIGILL)); + defineNativeProperty(vm, &module->values, "SIGFPE", NUMBER_VAL(SIGFPE)); + defineNativeProperty(vm, &module->values, "SIGKILL", NUMBER_VAL(SIGKILL)); + defineNativeProperty(vm, &module->values, "SIGSEGV", NUMBER_VAL(SIGSEGV)); + defineNativeProperty(vm, &module->values, "SIGTERM", NUMBER_VAL(SIGTERM)); + +#if defined(__Linux__) + defineNativeProperty(vm, &module->values, "SIGHUP", NUMBER_VAL(1)); + defineNativeProperty(vm, &module->values, "SIGQUIT", NUMBER_VAL(3)); + defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(6)); + defineNativeProperty(vm, &module->values, "SIGTRAP", NUMBER_VAL(5)); + defineNativeProperty(vm, &module->values, "SIGIOT", NUMBER_VAL(6)); + defineNativeProperty(vm, &module->values, "SIGBUS", NUMBER_VAL(7)); + defineNativeProperty(vm, &module->values, "SIGUSR1", NUMBER_VAL(10)); + defineNativeProperty(vm, &module->values, "SIGUSR2", NUMBER_VAL(12)); + defineNativeProperty(vm, &module->values, "SIGPIPE", NUMBER_VAL(13)); + defineNativeProperty(vm, &module->values, "SIGALRM", NUMBER_VAL(14)); + defineNativeProperty(vm, &module->values, "SIGSTKFLT", NUMBER_VAL(16)); + defineNativeProperty(vm, &module->values, "SIGCHLD", NUMBER_VAL(17)); + defineNativeProperty(vm, &module->values, "SIGCONT", NUMBER_VAL(18)); + defineNativeProperty(vm, &module->values, "SIGSTOP", NUMBER_VAL(19)); + defineNativeProperty(vm, &module->values, "SIGTSTP", NUMBER_VAL(20)); + defineNativeProperty(vm, &module->values, "SIGTTIN", NUMBER_VAL(21)); + defineNativeProperty(vm, &module->values, "SIGTTOU", NUMBER_VAL(22)); + defineNativeProperty(vm, &module->values, "SIGURG", NUMBER_VAL(23)); + defineNativeProperty(vm, &module->values, "SIGXCPU", NUMBER_VAL(24)); + defineNativeProperty(vm, &module->values, "SIGXFSZ", NUMBER_VAL(25)); + defineNativeProperty(vm, &module->values, "SIGVTALRM", NUMBER_VAL(26)); + defineNativeProperty(vm, &module->values, "SIGPROF", NUMBER_VAL(27)); + defineNativeProperty(vm, &module->values, "SIGWINCH", NUMBER_VAL(28)); + defineNativeProperty(vm, &module->values, "SIGIO", NUMBER_VAL(29)); + defineNativeProperty(vm, &module->values, "SIGPWR", NUMBER_VAL(30)); + defineNativeProperty(vm, &module->values, "SIGSYS", NUMBER_VAL(31)); + defineNativeProperty(vm, &module->values, "SIGUNUSED", NUMBER_VAL(31)); +#elif defined(__FreeBSD__) || defined(__APPLE__) + defineNativeProperty(vm, &module->values, "SIGHUP", NUMBER_VAL(1)); + defineNativeProperty(vm, &module->values, "SIGQUIT", NUMBER_VAL(3)); + defineNativeProperty(vm, &module->values, "SIGTRAP", NUMBER_VAL(5)); + defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(6)); + defineNativeProperty(vm, &module->values, "SIGEMT", NUMBER_VAL(7)); + defineNativeProperty(vm, &module->values, "SIGBUS", NUMBER_VAL(10)); + defineNativeProperty(vm, &module->values, "SIGSYS", NUMBER_VAL(12)); + defineNativeProperty(vm, &module->values, "SIGPIPE", NUMBER_VAL(13)); + defineNativeProperty(vm, &module->values, "SIGALRM", NUMBER_VAL(14)); + defineNativeProperty(vm, &module->values, "SIGURG", NUMBER_VAL(16)); + defineNativeProperty(vm, &module->values, "SIGSTOP", NUMBER_VAL(17)); + defineNativeProperty(vm, &module->values, "SIGTSTP", NUMBER_VAL(18)); + defineNativeProperty(vm, &module->values, "SIGCONT", NUMBER_VAL(19)); + defineNativeProperty(vm, &module->values, "SIGCHLD", NUMBER_VAL(20)); + defineNativeProperty(vm, &module->values, "SIGTTIN", NUMBER_VAL(21)); + defineNativeProperty(vm, &module->values, "SIGTTOU", NUMBER_VAL(22)); + defineNativeProperty(vm, &module->values, "SIGIO", NUMBER_VAL(23)); + defineNativeProperty(vm, &module->values, "SIGXCPU", NUMBER_VAL(24)); + defineNativeProperty(vm, &module->values, "SIGXFSZ", NUMBER_VAL(25)); + defineNativeProperty(vm, &module->values, "SIGVTALRM", NUMBER_VAL(26)); + defineNativeProperty(vm, &module->values, "SIGPROF", NUMBER_VAL(27)); + defineNativeProperty(vm, &module->values, "SIGWINCH", NUMBER_VAL(28)); + defineNativeProperty(vm, &module->values, "SIGINFO", NUMBER_VAL(29)); + defineNativeProperty(vm, &module->values, "SIGUSR1", NUMBER_VAL(30)); + defineNativeProperty(vm, &module->values, "SIGUSR2", NUMBER_VAL(31)); + defineNativeProperty(vm, &module->values, "SIGTHR", NUMBER_VAL(32)); + defineNativeProperty(vm, &module->values, "SIGLIBRT", NUMBER_VAL(33)); +#elif defined(__WIN32) + defineNativeProperty(vm, &module->values, "SIGEXIT", NUMBER_VAL(0)); + defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(22)); +#endif pop(vm); pop(vm); From 55b5a7ffd031230d9fcc17eea1007907bbbcbc18 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:19:57 -0700 Subject: [PATCH 02/12] fix macro Signed-off-by: Brian Downs --- src/optionals/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index aff8c19d..d178a0e3 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -377,7 +377,7 @@ Value createProcessModule(DictuVM* vm) { defineNativeProperty(vm, &module->values, "SIGUSR2", NUMBER_VAL(31)); defineNativeProperty(vm, &module->values, "SIGTHR", NUMBER_VAL(32)); defineNativeProperty(vm, &module->values, "SIGLIBRT", NUMBER_VAL(33)); -#elif defined(__WIN32) +#elif defined(WIN32) defineNativeProperty(vm, &module->values, "SIGEXIT", NUMBER_VAL(0)); defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(22)); #endif From 7c90c64096fcedd603b496b0dcb797d306917fcd Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:25:19 -0700 Subject: [PATCH 03/12] fix macro Signed-off-by: Brian Downs --- src/optionals/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index d178a0e3..3a72d67a 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -377,7 +377,7 @@ Value createProcessModule(DictuVM* vm) { defineNativeProperty(vm, &module->values, "SIGUSR2", NUMBER_VAL(31)); defineNativeProperty(vm, &module->values, "SIGTHR", NUMBER_VAL(32)); defineNativeProperty(vm, &module->values, "SIGLIBRT", NUMBER_VAL(33)); -#elif defined(WIN32) +#elif _WIN32 defineNativeProperty(vm, &module->values, "SIGEXIT", NUMBER_VAL(0)); defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(22)); #endif From acb17952e8bec59e15060a1f86bed5852aa7e576 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:26:03 -0700 Subject: [PATCH 04/12] fix macro Signed-off-by: Brian Downs --- src/optionals/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index 3a72d67a..5e017e54 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -377,7 +377,7 @@ Value createProcessModule(DictuVM* vm) { defineNativeProperty(vm, &module->values, "SIGUSR2", NUMBER_VAL(31)); defineNativeProperty(vm, &module->values, "SIGTHR", NUMBER_VAL(32)); defineNativeProperty(vm, &module->values, "SIGLIBRT", NUMBER_VAL(33)); -#elif _WIN32 +#elif defined(_WIN32) defineNativeProperty(vm, &module->values, "SIGEXIT", NUMBER_VAL(0)); defineNativeProperty(vm, &module->values, "SIGABRT", NUMBER_VAL(22)); #endif From 8f6b4a15940080276c0b29daffca84f188edcf62 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:43:48 -0700 Subject: [PATCH 05/12] fix macro Signed-off-by: Brian Downs --- src/optionals/process.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index 5e017e54..8a41de90 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -314,12 +314,12 @@ Value createProcessModule(DictuVM* vm) { /** * Define process properties */ - defineNativeProperty(vm, &module->values, "SIGINT", NUMBER_VAL(SIGINT)); - defineNativeProperty(vm, &module->values, "SIGILL", NUMBER_VAL(SIGILL)); - defineNativeProperty(vm, &module->values, "SIGFPE", NUMBER_VAL(SIGFPE)); - defineNativeProperty(vm, &module->values, "SIGKILL", NUMBER_VAL(SIGKILL)); - defineNativeProperty(vm, &module->values, "SIGSEGV", NUMBER_VAL(SIGSEGV)); - defineNativeProperty(vm, &module->values, "SIGTERM", NUMBER_VAL(SIGTERM)); + defineNativeProperty(vm, &module->values, "SIGINT", NUMBER_VAL(2)); + defineNativeProperty(vm, &module->values, "SIGILL", NUMBER_VAL(4)); + defineNativeProperty(vm, &module->values, "SIGFPE", NUMBER_VAL(8)); + defineNativeProperty(vm, &module->values, "SIGKILL", NUMBER_VAL(9)); + defineNativeProperty(vm, &module->values, "SIGSEGV", NUMBER_VAL(11)); + defineNativeProperty(vm, &module->values, "SIGTERM", NUMBER_VAL(15)); #if defined(__Linux__) defineNativeProperty(vm, &module->values, "SIGHUP", NUMBER_VAL(1)); From 2ae3422ead021565234ef65a57d1f3258132bae6 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:47:02 -0700 Subject: [PATCH 06/12] add pid_t macro Signed-off-by: Brian Downs --- src/optionals/process.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/optionals/process.c b/src/optionals/process.c index 8a41de90..89cc04dd 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -2,6 +2,10 @@ #include "process.h" +#ifdef _WIN32 +#define pid_t int +#endif + #ifdef _WIN32 static char* buildArgs(DictuVM *vm, ObjList* list, int *size) { // 3 for 1st arg escape + null terminator From 798c3712870b2e6193251b38af1c5979960e20a6 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 6 Nov 2023 20:52:40 -0700 Subject: [PATCH 07/12] explicit sigkill value Signed-off-by: Brian Downs --- src/optionals/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index 89cc04dd..00c3dbed 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -286,7 +286,7 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { } pid_t pid = (pid_t)AS_NUMBER(args[0]); - int signal = SIGKILL; + int signal = 9; if (argCount == 2) { if (!IS_NUMBER(args[1])) { From 49fe0c2b04dce87bbb7f0af9dae9f338d9838aa9 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 7 Nov 2023 15:09:31 -0700 Subject: [PATCH 08/12] windows madness Signed-off-by: Brian Downs --- src/optionals/process.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index 00c3dbed..02b390bc 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -1,4 +1,7 @@ #include +#ifdef _WIN32 +#include "windowsapi.h" +#endif #include "process.h" @@ -297,9 +300,17 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { signal = AS_NUMBER(args[1]); } - int res = kill(pid, signal); +#ifdef _WIN32 + HANDLE handle = OpenProcess(PROCESS_TERMINATE, TRUE, (int)pid); + if (handle != NULL) { + TerminateProcess(handle, 0); + CloseHandle(handle); + } +#else + kill(pid, signal); +#endif - return newResultSuccess(vm, AS_NUMBER(res)); + return newResultSuccess(vm, NIL_VAL); } Value createProcessModule(DictuVM* vm) { From 3354065e4adba83b90cbab04126b74e7ee889c59 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 7 Nov 2023 15:22:23 -0700 Subject: [PATCH 09/12] seperate impl for windows, update docs Signed-off-by: Brian Downs --- docs/docs/standard-lib/process.md | 2 ++ src/optionals/process.c | 32 +++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/docs/docs/standard-lib/process.md b/docs/docs/standard-lib/process.md index 3694c933..2f6616b5 100644 --- a/docs/docs/standard-lib/process.md +++ b/docs/docs/standard-lib/process.md @@ -64,3 +64,5 @@ const res = Process.kill(709871); const res = Process.kill(709871, Process.SIGTERM).unwrap(); // 0 ``` + +**Note:** On Windows, `kill` only takes the PID as the argument. diff --git a/src/optionals/process.c b/src/optionals/process.c index 02b390bc..6f50f074 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -277,6 +277,30 @@ static Value runProcess(DictuVM* vm, int argCount, Value* args) { return execute(vm, argList, true); } +#ifdef _WIN32 +static Value killProcess(DictuVM* vm, int argCount, Value* args) { + if (argCount > 2) { + runtimeError(vm, "kill() takes 1 argument (%d given)", argCount); + return EMPTY_VAL; + } + + if (!IS_NUMBER(args[0])) { + runtimeError(vm, "Argument passed to kill() must be a number"); + return EMPTY_VAL; + } + + pid_t pid = (pid_t)AS_NUMBER(args[0]); + + HANDLE handle = OpenProcess(PROCESS_TERMINATE, TRUE, (int)pid); + if (handle != NULL) { + TerminateProcess(handle, 0); + CloseHandle(handle); + } + + return newResultSuccess(vm, NIL_VAL); +} +#endif + static Value killProcess(DictuVM* vm, int argCount, Value* args) { if (argCount > 2) { runtimeError(vm, "kill() takes 1 or 2 arguments (%d given)", argCount); @@ -300,15 +324,7 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { signal = AS_NUMBER(args[1]); } -#ifdef _WIN32 - HANDLE handle = OpenProcess(PROCESS_TERMINATE, TRUE, (int)pid); - if (handle != NULL) { - TerminateProcess(handle, 0); - CloseHandle(handle); - } -#else kill(pid, signal); -#endif return newResultSuccess(vm, NIL_VAL); } From 1124f18ab8c079460e33826c2c7144cbd4b7def4 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 7 Nov 2023 15:27:08 -0700 Subject: [PATCH 10/12] more preprocessor segmentation Signed-off-by: Brian Downs --- src/optionals/process.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/optionals/process.c b/src/optionals/process.c index 6f50f074..ca63f886 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -299,8 +299,7 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { return newResultSuccess(vm, NIL_VAL); } -#endif - +#else static Value killProcess(DictuVM* vm, int argCount, Value* args) { if (argCount > 2) { runtimeError(vm, "kill() takes 1 or 2 arguments (%d given)", argCount); @@ -328,6 +327,7 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { return newResultSuccess(vm, NIL_VAL); } +#endif Value createProcessModule(DictuVM* vm) { ObjString* name = copyString(vm, "Process", 7); From 66cee3ed1228c8250956b0ca5565e636423f047d Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Thu, 9 Nov 2023 15:40:22 -0700 Subject: [PATCH 11/12] add kill tests for process module Signed-off-by: Brian Downs --- docs/docs/standard-lib/process.md | 2 +- src/optionals/process.c | 4 ++- tests/process/exec.du | 2 +- tests/process/import.du | 2 +- tests/process/kill.du | 46 +++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/process/kill.du diff --git a/docs/docs/standard-lib/process.md b/docs/docs/standard-lib/process.md index 2f6616b5..165f8b1c 100644 --- a/docs/docs/standard-lib/process.md +++ b/docs/docs/standard-lib/process.md @@ -51,7 +51,7 @@ Process.run(["ls", "-la"]).unwrap(); print(Process.run(["echo", "test"], true).unwrap()); // 'test' ``` -### Process.kill(Number, Number -> Optional) -> Result\ +### Process.kill(Number, Number -> Optional) -> Result\ kill receives a process ID number and an optional signal number and attempts to kill the process associated with the given pid. If no signal is provided, SIGKILL is used. diff --git a/src/optionals/process.c b/src/optionals/process.c index ca63f886..ec7de1f5 100644 --- a/src/optionals/process.c +++ b/src/optionals/process.c @@ -323,7 +323,9 @@ static Value killProcess(DictuVM* vm, int argCount, Value* args) { signal = AS_NUMBER(args[1]); } - kill(pid, signal); + if (kill(pid, signal) == -1) { + ERROR_RESULT; + } return newResultSuccess(vm, NIL_VAL); } diff --git a/tests/process/exec.du b/tests/process/exec.du index 853162bd..835c0805 100644 --- a/tests/process/exec.du +++ b/tests/process/exec.du @@ -25,4 +25,4 @@ class TestProcessExec < UnitTest { } } -TestProcessExec().run(); \ No newline at end of file +TestProcessExec().run(); diff --git a/tests/process/import.du b/tests/process/import.du index 894e9ea7..d81d9fe1 100644 --- a/tests/process/import.du +++ b/tests/process/import.du @@ -5,4 +5,4 @@ */ import "exec.du"; -import "run.du"; \ No newline at end of file +import "run.du"; diff --git a/tests/process/kill.du b/tests/process/kill.du new file mode 100644 index 00000000..2328522c --- /dev/null +++ b/tests/process/kill.du @@ -0,0 +1,46 @@ +/** + * kill.du + * + * Testing the Process.kill() function + * + * kill() receives a PID as a Number and optional signal, as a Number (on *NIX) to be passed to the process. + */ +from UnitTest import UnitTest; + +import IO; +import Process; +import System; + +class TestProcessKill < UnitTest { + testProcessKillError() { + const res = Process.kill(9999999999999999).unwrapError(); + this.assertEquals(res, "No such process"); + } + + testProcessKillNoSignal() { + Process.exec(["sleep", "100"]); + + const out = Process.run(["pgrep", "sleep"], true).unwrap(); + const pids = out.split("\n"); + + if (pids.len() > 1) { + for (var i = 0; i < pids.len(); i += 1) { + var pid = pids[i].replace("\n", ""); + pid = pid.toNumber().unwrap(); + const res = Process.kill(pid, 0); + this.assertSuccess(res); + this.assertNil(res.unwrap()); + } + } else { + var pid = pids[0].replace("\n", ""); + pid = pid.toNumber().unwrap(); + const res = Process.kill(pid, 0); + this.assertSuccess(res); + this.assertNil(res.unwrap()); + } + } +} + +if (System.platform != "windows") { + TestProcessKill().run(); +} From d9101f3da96947d6c57aad31fe6fe117e932e3d1 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Thu, 9 Nov 2023 16:09:10 -0700 Subject: [PATCH 12/12] add kill.du to tests Signed-off-by: Brian Downs --- tests/process/import.du | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/process/import.du b/tests/process/import.du index d81d9fe1..158164f6 100644 --- a/tests/process/import.du +++ b/tests/process/import.du @@ -5,4 +5,5 @@ */ import "exec.du"; +import "kill.du"; import "run.du";