robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
87b5062f94
master
1/// OS process functions 2/// 3/// psutil is a good reference for cross-platform syscall voodoo: 4/// https://github.com/giampaolo/psutil/tree/master/psutil/arch 5 6// IWYU pragma: no_include <sys/param.h> 7 8#include <assert.h> 9#include <signal.h> 10#include <stdbool.h> 11#include <stddef.h> 12#include <uv.h> 13 14#ifdef MSWIN 15# include <tlhelp32.h> 16#endif 17 18#ifdef __FreeBSD__ 19# include <string.h> 20# include <sys/types.h> 21# include <sys/user.h> 22#endif 23 24#if defined(__NetBSD__ )|| defined(__OpenBSD__ ) 25# include <sys/param.h> 26#endif 27 28// Exclude GNU Hurd since glibc defines BSD but lacks sysctl.h 29#if defined(__APPLE__ )|| (defined(BSD )&& !defined(__gnu_hurd__ )) 30# include <sys/sysctl.h> 31 32# include "nvim/macros_defs.h" 33#endif 34 35// Group Hurd with Linux, as both use glibc and standard POSIX interfaces here 36#if defined(__linux__ )|| defined(__gnu_hurd__ ) 37# include <stdio.h> 38#endif 39 40#include "nvim/log.h" 41#include "nvim/memory.h" 42#include "nvim/os/proc.h" 43 44#ifdef MSWIN 45# include "nvim/api/private/helpers.h" 46#endif 47 48#include "os/proc.c.generated.h" 49 50#ifdef MSWIN 51static bool os_proc_tree_kill_rec (void * proc ,int sig ) 52{ 53if (proc == NULL ) { 54return false; 55 } 56PROCESSENTRY32 pe ; 57DWORD pid = GetProcessId (proc ); 58 59if (pid != 0 ) { 60HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS ,0 ); 61if (h != INVALID_HANDLE_VALUE ) { 62pe .dwSize = sizeof (PROCESSENTRY32 ); 63if (!Process32First (h ,& pe )) { 64 gototheend ; 65 } 66do { 67if (pe .th32ParentProcessID == pid ) { 68HANDLE ph = OpenProcess (PROCESS_ALL_ACCESS , false,pe .th32ProcessID ); 69if (ph != NULL ) { 70os_proc_tree_kill_rec (ph ,sig ); 71CloseHandle (ph ); 72 } 73 } 74 }while (Process32Next (h ,& pe )); 75CloseHandle (h ); 76 } 77 } 78 79theend : 80return (bool )TerminateProcess (proc , (unsigned )sig ); 81} 82/// Kills process `pid` and its descendants recursively. 83bool os_proc_tree_kill (int pid ,int sig ) 84{ 85assert (sig >=0 ); 86assert (sig == SIGTERM || sig == SIGKILL ); 87if (pid > 0 ) { 88ILOG ("terminating process tree: %d" ,pid ); 89HANDLE h = OpenProcess (PROCESS_ALL_ACCESS , false, (DWORD )pid ); 90return os_proc_tree_kill_rec (h ,sig ); 91 }else { 92ELOG ("invalid pid: %d" ,pid ); 93 } 94return false; 95} 96#else 97/// Kills process group where `pid` is the process group leader. 98bool os_proc_tree_kill (int pid ,int sig ) 99{ 100assert (sig == SIGTERM || sig == SIGKILL ); 101if (pid == 0 ) { 102// Never kill self (pid=0). 103return false; 104 } 105ILOG ("sending %s to PID %d" ,sig == SIGTERM ?"SIGTERM" :"SIGKILL" ,- pid ); 106return uv_kill (- pid ,sig )== 0 ; 107} 108#endif 109 110/// Gets the process ids of the immediate children of process `ppid`. 111/// 112/// @param ppid Process to inspect. 113/// @param[out,allocated] proc_list Child process ids. 114/// @param[out] proc_count Number of child processes. 115/// @return 0 on success, 1 if process not found, 2 on other error. 116int os_proc_children (int ppid ,int ** proc_list ,size_t * proc_count ) 117FUNC_ATTR_NONNULL_ALL 118{ 119if (ppid < 0 ) { 120return 2 ; 121 } 122 123int * temp = NULL ; 124* proc_list = NULL ; 125* proc_count = 0 ; 126 127#ifdef MSWIN 128PROCESSENTRY32 pe ; 129 130// Snapshot of all processes. 131HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS ,0 ); 132if (h == INVALID_HANDLE_VALUE ) { 133return 2 ; 134 } 135 136pe .dwSize = sizeof (PROCESSENTRY32 ); 137// Get root process. 138if (!Process32First (h ,& pe )) { 139CloseHandle (h ); 140return 2 ; 141 } 142// Collect processes whose parent matches `ppid`. 143do { 144if (pe .th32ParentProcessID == (DWORD )ppid ) { 145temp = xrealloc (temp , (* proc_count + 1 )* sizeof (* temp )); 146temp [* proc_count ]= (int )pe .th32ProcessID ; 147 (* proc_count )++ ; 148 } 149 }while (Process32Next (h ,& pe )); 150CloseHandle (h ); 151 152#elif defined(__APPLE__ )|| (defined(BSD )&& !defined(__gnu_hurd__ )) 153# if defined(__APPLE__ ) 154# define KP_PID (o ) o.kp_proc.p_pid 155# define KP_PPID (o ) o.kp_eproc.e_ppid 156# elif defined(__FreeBSD__ ) 157# define KP_PID (o ) o.ki_pid 158# define KP_PPID (o ) o.ki_ppid 159# else 160# define KP_PID (o ) o.p_pid 161# define KP_PPID (o ) o.p_ppid 162# endif 163# ifdef __NetBSD__ 164static int name []= { 165CTL_KERN ,KERN_PROC2 ,KERN_PROC_ALL ,0 , (int )(sizeof (struct kinfo_proc2 )),0 166 }; 167# else 168static int name []= {CTL_KERN ,KERN_PROC ,KERN_PROC_ALL ,0 }; 169# endif 170 171// Get total process count. 172size_t len = 0 ; 173int rv = sysctl (name ,ARRAY_SIZE (name )- 1 ,NULL ,& len ,NULL ,0 ); 174if (rv ) { 175return 2 ; 176 } 177 178// Get ALL processes. 179# ifdef __NetBSD__ 180struct kinfo_proc2 * p_list = xmalloc (len ); 181# else 182struct kinfo_proc * p_list = xmalloc (len ); 183# endif 184rv = sysctl (name ,ARRAY_SIZE (name )- 1 ,p_list ,& len ,NULL ,0 ); 185if (rv ) { 186xfree (p_list ); 187return 2 ; 188 } 189 190// Collect processes whose parent matches `ppid`. 191bool exists = false; 192size_t p_count = len /sizeof (* p_list ); 193for (size_t i = 0 ;i < p_count ;i ++ ) { 194exists = exists || KP_PID (p_list [i ])== ppid ; 195if (KP_PPID (p_list [i ])== ppid ) { 196temp = xrealloc (temp , (* proc_count + 1 )* sizeof (* temp )); 197temp [* proc_count ]= KP_PID (p_list [i ]); 198 (* proc_count )++ ; 199 } 200 } 201xfree (p_list ); 202if (!exists ) { 203return 1 ;// Process not found. 204 } 205 206#elif defined(__linux__ )|| defined(__gnu_hurd__ ) 207char proc_p [256 ]= {0 }; 208// Collect processes whose parent matches `ppid`. 209// Rationale: children are defined in thread with same ID of process. 210snprintf (proc_p ,sizeof (proc_p ),"/proc/%d/task/%d/children" ,ppid ,ppid ); 211FILE * fp = fopen (proc_p ,"r" ); 212if (fp == NULL ) { 213return 2 ;// Process not found, or /proc/…/children not supported. 214 } 215int match_pid ; 216while (fscanf (fp ,"%d" ,& match_pid )> 0 ) { 217temp = xrealloc (temp , (* proc_count + 1 )* sizeof (* temp )); 218temp [* proc_count ]= match_pid ; 219 (* proc_count )++ ; 220 } 221fclose (fp ); 222#endif 223 224* proc_list = temp ; 225return 0 ; 226} 227 228#ifdef MSWIN 229/// Gets various properties of the process identified by `pid`. 230/// 231/// @param pid Process to inspect. 232/// @return Map of process properties, empty on error. 233Dict os_proc_info (int pid ,Arena * arena ) 234{ 235Dict pinfo = ARRAY_DICT_INIT ; 236PROCESSENTRY32 pe ; 237 238// Snapshot of all processes. This is used instead of: 239// OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, …) 240// to avoid ERROR_PARTIAL_COPY. https://stackoverflow.com/a/29942376 241HANDLE h = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS ,0 ); 242if (h == INVALID_HANDLE_VALUE ) { 243return pinfo ;// Return empty. 244 } 245 246pe .dwSize = sizeof (PROCESSENTRY32 ); 247// Get root process. 248if (!Process32First (h ,& pe )) { 249CloseHandle (h ); 250return pinfo ;// Return empty. 251 } 252// Find the process. 253do { 254if (pe .th32ProcessID == (DWORD )pid ) { 255break ; 256 } 257 }while (Process32Next (h ,& pe )); 258CloseHandle (h ); 259 260if (pe .th32ProcessID == (DWORD )pid ) { 261pinfo = arena_dict (arena ,3 ); 262PUT_C (pinfo ,"pid" ,INTEGER_OBJ (pid )); 263PUT_C (pinfo ,"ppid" ,INTEGER_OBJ ((int )pe .th32ParentProcessID )); 264PUT_C (pinfo ,"name" ,CSTR_TO_ARENA_OBJ (arena ,pe .szExeFile )); 265 } 266 267return pinfo ; 268} 269#endif 270 271/// Return true if process `pid` is running. 272bool os_proc_running (int pid ) 273{ 274int err = uv_kill (pid ,0 ); 275// If there is no error the process must be running. 276if (err == 0 ) { 277return true; 278 } 279// If the error is ESRCH then the process is not running. 280if (err == UV_ESRCH ) { 281return false; 282 } 283// If the process is running and owned by another user we get EPERM. With 284// other errors the process might be running, assuming it is then. 285return true; 286}