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;
+}
This commit is contained in:
Justin M. Keyes
2018-03-16 05:13:38 +01:00
parent 330e5acbce
commit a034d4b69d
6 changed files with 179 additions and 29 deletions

View File

@@ -1482,11 +1482,11 @@ Array nvim_list_uis(void)
/// Gets the immediate children of process `pid`. /// Gets the immediate children of process `pid`.
/// ///
/// @return Array of child process ids. Empty array if process not found. /// @return Array of child process ids, empty if process not found.
Array nvim_get_proc_children(Integer pid, Error *err) Array nvim_get_proc_children(Integer pid, Error *err)
FUNC_API_SINCE(4) FUNC_API_SINCE(4)
{ {
Array proc_array = ARRAY_DICT_INIT; Array rvobj = ARRAY_DICT_INIT;
int *proc_list = NULL; int *proc_list = NULL;
if (pid <= 0 || pid > INT_MAX) { if (pid <= 0 || pid > INT_MAX) {
@@ -1498,6 +1498,7 @@ Array nvim_get_proc_children(Integer pid, Error *err)
int rv = os_proc_children((int)pid, &proc_list, &proc_count); int rv = os_proc_children((int)pid, &proc_list, &proc_count);
if (rv != 0) { if (rv != 0) {
// syscall failed (possibly because of kernel options), try shelling out. // syscall failed (possibly because of kernel options), try shelling out.
DLOG("fallback to vim._os_proc_children()");
Array a = ARRAY_DICT_INIT; Array a = ARRAY_DICT_INIT;
ADD(a, INTEGER_OBJ(pid)); ADD(a, INTEGER_OBJ(pid));
String s = cstr_to_string("return vim._os_proc_children(select(1, ...))"); String s = cstr_to_string("return vim._os_proc_children(select(1, ...))");
@@ -1505,7 +1506,7 @@ Array nvim_get_proc_children(Integer pid, Error *err)
api_free_string(s); api_free_string(s);
api_free_array(a); api_free_array(a);
if (o.type == kObjectTypeArray) { if (o.type == kObjectTypeArray) {
proc_array = o.data.array; rvobj = o.data.array;
} else if (!ERROR_SET(err)) { } else if (!ERROR_SET(err)) {
api_set_error(err, kErrorTypeException, api_set_error(err, kErrorTypeException,
"Failed to get process children. pid=%" PRId64 " error=%d", "Failed to get process children. pid=%" PRId64 " error=%d",
@@ -1515,10 +1516,49 @@ Array nvim_get_proc_children(Integer pid, Error *err)
} }
for (size_t i = 0; i < proc_count; i++) { for (size_t i = 0; i < proc_count; i++) {
ADD(proc_array, INTEGER_OBJ(proc_list[i])); ADD(rvobj, INTEGER_OBJ(proc_list[i]));
} }
end: end:
xfree(proc_list); xfree(proc_list);
return proc_array; return rvobj;
}
/// Gets info describing process `pid`.
///
/// @return Map of process properties, or NIL if process not found.
Object nvim_get_proc(Integer pid, Error *err)
FUNC_API_SINCE(4)
{
Object rvobj = OBJECT_INIT;
rvobj.data.dictionary = (Dictionary)ARRAY_DICT_INIT;
rvobj.type = kObjectTypeDictionary;
if (pid <= 0 || pid > INT_MAX) {
api_set_error(err, kErrorTypeException, "Invalid pid: %" PRId64, pid);
return NIL;
}
#ifdef WIN32
rvobj.data.dictionary = os_proc_info((int)pid);
if (rvobj.data.dictionary.size == 0) { // Process not found.
return NIL;
}
#else
// Cross-platform process info APIs are miserable, so use `ps` instead.
Array a = ARRAY_DICT_INIT;
ADD(a, INTEGER_OBJ(pid));
String s = cstr_to_string("return vim._os_proc_info(select(1, ...))");
Object o = nvim_execute_lua(s, a, err);
api_free_string(s);
api_free_array(a);
if (o.type == kObjectTypeArray && o.data.array.size == 0) {
return NIL; // Process not found.
} else if (o.type == kObjectTypeDictionary) {
rvobj.data.dictionary = o.data.dictionary;
} else if (!ERROR_SET(err)) {
api_set_error(err, kErrorTypeException,
"Failed to get process info. pid=%" PRId64, pid);
}
#endif
return rvobj;
} }

View File

@@ -1,18 +1,54 @@
-- Gets the children of process `ppid` via the shell. -- Internal-only until comments in #8107 are addressed.
-- Returns:
-- {errcode}, {output}
local function _system(cmd)
local out = vim.api.nvim_call_function('system', { cmd })
local err = vim.api.nvim_get_vvar('shell_error')
return err, out
end
-- Gets process info from the `ps` command.
-- Used by nvim_get_proc() as a fallback.
local function _os_proc_info(pid)
if pid == nil or pid <= 0 or type(pid) ~= 'number' then
error('invalid pid')
end
local cmd = { 'ps', '-p', pid, '-o', 'ucomm=', }
local err, name = _system(cmd)
if 1 == err and string.gsub(name, '%s*', '') == '' then
return {} -- Process not found.
elseif 0 ~= err then
local args_str = vim.api.nvim_call_function('string', { cmd })
error('command failed: '..args_str)
end
local _, ppid = _system({ 'ps', '-p', pid, '-o', 'ppid=', })
-- Remove trailing whitespace.
name = string.gsub(name, '%s+$', '')
ppid = string.gsub(ppid, '%s+$', '')
ppid = tonumber(ppid) == nil and -1 or tonumber(ppid)
return {
name = name,
pid = pid,
ppid = ppid,
}
end
-- Gets process children from the `pgrep` command.
-- Used by nvim_get_proc_children() as a fallback. -- Used by nvim_get_proc_children() as a fallback.
local function _os_proc_children(ppid) local function _os_proc_children(ppid)
if ppid == nil or ppid <= 0 or type(ppid) ~= 'number' then if ppid == nil or ppid <= 0 or type(ppid) ~= 'number' then
error('invalid ppid') error('invalid ppid')
end end
local out = vim.api.nvim_call_function('system', { 'pgrep -P '..ppid }) local cmd = { 'pgrep', '-P', ppid, }
local err = vim.api.nvim_get_vvar('shell_error') local err, rv = _system(cmd)
if 1 == err and out == '' then if 1 == err and string.gsub(rv, '%s*', '') == '' then
return {} -- Process not found. return {} -- Process not found.
elseif 0 ~= err then elseif 0 ~= err then
error('pgrep failed') local args_str = vim.api.nvim_call_function('string', { cmd })
error('command failed: '..args_str)
end end
local children = {} local children = {}
for s in string.gmatch(out, '%S+') do for s in string.gmatch(rv, '%S+') do
local i = tonumber(s) local i = tonumber(s)
if i ~= nil then if i ~= nil then
table.insert(children, i) table.insert(children, i)
@@ -81,8 +117,12 @@ local function _update_package_paths()
end end
last_nvim_paths = cur_nvim_paths last_nvim_paths = cur_nvim_paths
end end
--{{{1 Module definition
return { local module = {
_update_package_paths = _update_package_paths, _update_package_paths = _update_package_paths,
_os_proc_children = _os_proc_children, _os_proc_children = _os_proc_children,
_os_proc_info = _os_proc_info,
_system = _system,
} }
return module

View File

@@ -4,7 +4,7 @@
/// @file fileio.c /// @file fileio.c
/// ///
/// Buffered reading/writing to a file. Unlike fileio.c this is not dealing with /// Buffered reading/writing to a file. Unlike fileio.c this is not dealing with
/// Neovim stuctures for buffer, with autocommands, etc: just fopen/fread/fwrite /// Nvim stuctures for buffer, with autocommands, etc: just fopen/fread/fwrite
/// replacement. /// replacement.
#include <assert.h> #include <assert.h>
@@ -43,7 +43,7 @@
/// @param[in] mode Permissions for the newly created file (ignored if flags /// @param[in] mode Permissions for the newly created file (ignored if flags
/// does not have kFileCreate\*). /// does not have kFileCreate\*).
/// ///
/// @return Error code (@see os_strerror()) or 0. /// @return Error code, or 0 on success. @see os_strerror()
int file_open(FileDescriptor *const ret_fp, const char *const fname, int file_open(FileDescriptor *const ret_fp, const char *const fname,
const int flags, const int mode) const int flags, const int mode)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
@@ -115,8 +115,7 @@ int file_open_fd(FileDescriptor *const ret_fp, const int fd, const bool wr)
/// Like file_open(), but allocate and return ret_fp /// Like file_open(), but allocate and return ret_fp
/// ///
/// @param[out] error Error code, @see os_strerror(). Is set to zero on /// @param[out] error Error code, or 0 on success. @see os_strerror()
/// success.
/// @param[in] fname File name to open. /// @param[in] fname File name to open.
/// @param[in] flags Flags, @see FileOpenFlags. /// @param[in] flags Flags, @see FileOpenFlags.
/// @param[in] mode Permissions for the newly created file (ignored if flags /// @param[in] mode Permissions for the newly created file (ignored if flags
@@ -137,8 +136,7 @@ FileDescriptor *file_open_new(int *const error, const char *const fname,
/// Like file_open_fd(), but allocate and return ret_fp /// Like file_open_fd(), but allocate and return ret_fp
/// ///
/// @param[out] error Error code, @see os_strerror(). Is set to zero on /// @param[out] error Error code, or 0 on success. @see os_strerror()
/// success.
/// @param[in] fd File descriptor to wrap. /// @param[in] fd File descriptor to wrap.
/// @param[in] wr True if fd is opened for writing only, false if it is read /// @param[in] wr True if fd is opened for writing only, false if it is read
/// only. /// only.

View File

@@ -1,6 +1,11 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check // This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/// OS process functions
///
/// psutil is a good reference for cross-platform syscall voodoo:
/// https://github.com/giampaolo/psutil/tree/master/psutil/arch
#include <uv.h> // for HANDLE (win32) #include <uv.h> // for HANDLE (win32)
#ifdef WIN32 #ifdef WIN32
@@ -18,10 +23,12 @@
# include <pwd.h> # include <pwd.h>
#endif #endif
#include "nvim/globals.h"
#include "nvim/log.h" #include "nvim/log.h"
#include "nvim/os/process.h" #include "nvim/os/process.h"
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/os/os_defs.h" #include "nvim/os/os_defs.h"
#include "nvim/api/private/helpers.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/process.c.generated.h" # include "os/process.c.generated.h"
@@ -104,11 +111,6 @@ bool os_proc_tree_kill(int pid, int sig)
/// @return 0 on success, 1 if process not found, 2 on other error. /// @return 0 on success, 1 if process not found, 2 on other error.
int os_proc_children(int ppid, int **proc_list, size_t *proc_count) int os_proc_children(int ppid, int **proc_list, size_t *proc_count)
{ {
//
// psutil is a good reference for cross-platform syscall voodoo:
// https://github.com/giampaolo/psutil/tree/master/psutil/arch
//
if (ppid < 0) { if (ppid < 0) {
return 2; return 2;
} }
@@ -208,3 +210,44 @@ int os_proc_children(int ppid, int **proc_list, size_t *proc_count)
return 0; return 0;
} }
#ifdef WIN32
/// 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;
PROCESSENTRY32 pe;
// Snapshot of all processes. This is used instead of:
// OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, …)
// to avoid ERROR_PARTIAL_COPY. https://stackoverflow.com/a/29942376
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (h == INVALID_HANDLE_VALUE) {
return pinfo; // Return empty.
}
pe.dwSize = sizeof(PROCESSENTRY32);
// Get root process.
if (!Process32First(h, &pe)) {
CloseHandle(h);
return pinfo; // Return empty.
}
// Find the process.
do {
if (pe.th32ProcessID == (DWORD)pid) {
break;
}
} while (Process32Next(h, &pe));
CloseHandle(h);
if (pe.th32ProcessID == (DWORD)pid) {
PUT(pinfo, "pid", INTEGER_OBJ(pid));
PUT(pinfo, "ppid", INTEGER_OBJ((int)pe.th32ParentProcessID));
PUT(pinfo, "name", STRING_OBJ(cstr_to_string(pe.szExeFile)));
}
return pinfo;
}
#endif

View File

@@ -2,6 +2,7 @@
#define NVIM_OS_PROCESS_H #define NVIM_OS_PROCESS_H
#include <stddef.h> #include <stddef.h>
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/process.h.generated.h" # include "os/process.h.generated.h"

View File

@@ -1,10 +1,14 @@
local helpers = require('test.functional.helpers')(after_each) 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 funcs = helpers.funcs
local iswin = helpers.iswin
local nvim_argv = helpers.nvim_argv local nvim_argv = helpers.nvim_argv
local ok = helpers.ok
local request = helpers.request local request = helpers.request
local retry = helpers.retry local retry = helpers.retry
local NIL = helpers.NIL
describe('api', function() describe('api', function()
before_each(clear) before_each(clear)
@@ -43,11 +47,35 @@ describe('api', function()
eq(false, status) eq(false, status)
eq("Invalid pid: 0", string.match(rv, "Invalid.*")) eq("Invalid pid: 0", string.match(rv, "Invalid.*"))
-- Assume PID 99999999 does not exist. -- Assume PID 99999 does not exist.
status, rv = pcall(request, "nvim_get_proc_children", 99999999) status, rv = pcall(request, "nvim_get_proc_children", 99999)
eq(true, status) eq(true, status)
eq({}, rv) eq({}, rv)
end) end)
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) end)