mirror of
https://github.com/neovim/neovim.git
synced 2025-09-30 15:08:35 +00:00
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:
@@ -4,7 +4,7 @@
|
||||
/// @file fileio.c
|
||||
///
|
||||
/// 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.
|
||||
|
||||
#include <assert.h>
|
||||
@@ -43,7 +43,7 @@
|
||||
/// @param[in] mode Permissions for the newly created file (ignored if flags
|
||||
/// 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,
|
||||
const int flags, const int mode)
|
||||
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
|
||||
///
|
||||
/// @param[out] error Error code, @see os_strerror(). Is set to zero on
|
||||
/// success.
|
||||
/// @param[out] error Error code, or 0 on success. @see os_strerror()
|
||||
/// @param[in] fname File name to open.
|
||||
/// @param[in] flags Flags, @see FileOpenFlags.
|
||||
/// @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
|
||||
///
|
||||
/// @param[out] error Error code, @see os_strerror(). Is set to zero on
|
||||
/// success.
|
||||
/// @param[out] error Error code, or 0 on success. @see os_strerror()
|
||||
/// @param[in] fd File descriptor to wrap.
|
||||
/// @param[in] wr True if fd is opened for writing only, false if it is read
|
||||
/// only.
|
||||
|
Reference in New Issue
Block a user