diff options
Diffstat (limited to 'src/nvim/api/vim.c')
| -rw-r--r-- | src/nvim/api/vim.c | 50 | 
1 files changed, 45 insertions, 5 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 9f1f395c28..962081cc23 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1482,11 +1482,11 @@ Array nvim_list_uis(void)  /// 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)    FUNC_API_SINCE(4)  { -  Array proc_array = ARRAY_DICT_INIT; +  Array rvobj = ARRAY_DICT_INIT;    int *proc_list = NULL;    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);    if (rv != 0) {      // syscall failed (possibly because of kernel options), try shelling out. +    DLOG("fallback to vim._os_proc_children()");      Array a = ARRAY_DICT_INIT;      ADD(a, INTEGER_OBJ(pid));      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_array(a);      if (o.type == kObjectTypeArray) { -      proc_array = o.data.array; +      rvobj = o.data.array;      } else if (!ERROR_SET(err)) {        api_set_error(err, kErrorTypeException,                      "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++) { -    ADD(proc_array, INTEGER_OBJ(proc_list[i])); +    ADD(rvobj, INTEGER_OBJ(proc_list[i]));    }  end:    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;  }  | 
