aboutsummaryrefslogtreecommitdiff
path: root/test/functional/api/proc_spec.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2018-03-16 05:13:38 +0100
committerJustin M. Keyes <justinkz@gmail.com>2018-03-18 00:11:45 +0100
commita034d4b69d6032b3431c10b8a11c998551700fc2 (patch)
tree0ef47d8fc75b8f8101ae788af328698b674e1041 /test/functional/api/proc_spec.lua
parent330e5acbcec00d1bc43a9c1110c4325fa62ba9ad (diff)
downloadrneovim-a034d4b69d6032b3431c10b8a11c998551700fc2.tar.gz
rneovim-a034d4b69d6032b3431c10b8a11c998551700fc2.tar.bz2
rneovim-a034d4b69d6032b3431c10b8a11c998551700fc2.zip
API: nvim_get_proc()
TODO: "exepath" field (win32: QueryFullProcessImageName()) On unix-likes `ps` is used because the platform-specific APIs are a nightmare. For reference, below is a (incomplete) attempt: diff --git a/src/nvim/os/process.c b/src/nvim/os/process.c index 09769925aca5..99afbbf290c1 100644 --- a/src/nvim/os/process.c +++ b/src/nvim/os/process.c @@ -208,3 +210,60 @@ int os_proc_children(int ppid, int **proc_list, size_t *proc_count) return 0; } +/// Gets various properties of the process identified by `pid`. +/// +/// @param pid Process to inspect. +/// @return Map of process properties, empty on error. +Dictionary os_proc_info(int pid) +{ + Dictionary pinfo = ARRAY_DICT_INIT; +#ifdef WIN32 + +#elif defined(__APPLE__) + char buf[PROC_PIDPATHINFO_MAXSIZE]; + if (proc_pidpath(pid, buf, sizeof(buf))) { + name = getName(buf); + PUT(pinfo, "exepath", STRING_OBJ(cstr_to_string(buf))); + return name; + } else { + ILOG("proc_pidpath() failed for pid: %d", pid); + } +#elif defined(BSD) +# if defined(__FreeBSD__) +# define KP_COMM(o) o.ki_comm +# else +# define KP_COMM(o) o.p_comm +# endif + struct kinfo_proc *proc = kinfo_getproc(pid); + if (proc) { + PUT(pinfo, "name", cstr_to_string(KP_COMM(proc))); + xfree(proc); + } else { + ILOG("kinfo_getproc() failed for pid: %d", pid); + } + +#elif defined(__linux__) + char fname[256] = { 0 }; + char buf[MAXPATHL]; + snprintf(fname, sizeof(fname), "/proc/%d/comm", pid); + FILE *fp = fopen(fname, "r"); + // FileDescriptor *f = file_open_new(&error, fname, kFileReadOnly, 0); + // ptrdiff_t file_read(FileDescriptor *const fp, char *const ret_buf, + // const size_t size) + if (fp == NULL) { + ILOG("fopen() of /proc/%d/comm failed", pid); + } else { + size_t n = fread(buf, sizeof(char), sizeof(buf) - 1, fp); + if (n == 0) { + WLOG("fread() of /proc/%d/comm failed", pid); + } else { + size_t end = MIN(sizeof(buf) - 1, n); + end = (end > 0 && buf[end - 1] == '\n') ? end - 1 : end; + buf[end] = '\0'; + PUT(pinfo, "name", STRING_OBJ(cstr_to_string(buf))); + } + } + fclose(fp); +#endif + return pinfo; +}
Diffstat (limited to 'test/functional/api/proc_spec.lua')
-rw-r--r--test/functional/api/proc_spec.lua34
1 files changed, 31 insertions, 3 deletions
diff --git a/test/functional/api/proc_spec.lua b/test/functional/api/proc_spec.lua
index eee54f9465..d99c26b6c2 100644
--- a/test/functional/api/proc_spec.lua
+++ b/test/functional/api/proc_spec.lua
@@ -1,10 +1,14 @@
local helpers = require('test.functional.helpers')(after_each)
-local clear, eq = helpers.clear, helpers.eq
+local clear = helpers.clear
+local eq = helpers.eq
local funcs = helpers.funcs
+local iswin = helpers.iswin
local nvim_argv = helpers.nvim_argv
+local ok = helpers.ok
local request = helpers.request
local retry = helpers.retry
+local NIL = helpers.NIL
describe('api', function()
before_each(clear)
@@ -43,11 +47,35 @@ describe('api', function()
eq(false, status)
eq("Invalid pid: 0", string.match(rv, "Invalid.*"))
- -- Assume PID 99999999 does not exist.
- status, rv = pcall(request, "nvim_get_proc_children", 99999999)
+ -- Assume PID 99999 does not exist.
+ status, rv = pcall(request, "nvim_get_proc_children", 99999)
eq(true, status)
eq({}, rv)
end)
end)
+ describe('nvim_get_proc', function()
+ it('returns process info', function()
+ local pid = funcs.getpid()
+ local pinfo = request('nvim_get_proc', pid)
+ eq((iswin() and 'nvim.exe' or 'nvim'), pinfo.name)
+ ok(pinfo.pid == pid)
+ ok(type(pinfo.ppid) == 'number' and pinfo.ppid ~= pid)
+ end)
+
+ it('validates input', function()
+ local status, rv = pcall(request, "nvim_get_proc", -1)
+ eq(false, status)
+ eq("Invalid pid: -1", string.match(rv, "Invalid.*"))
+
+ status, rv = pcall(request, "nvim_get_proc", 0)
+ eq(false, status)
+ eq("Invalid pid: 0", string.match(rv, "Invalid.*"))
+
+ -- Assume PID 99999 does not exist.
+ status, rv = pcall(request, "nvim_get_proc", 99999)
+ eq(true, status)
+ eq(NIL, rv)
+ end)
+ end)
end)