inline int kill_process(pid_t pid) { HANDLE process_handle = ::OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (process_handle == nullptr) { LOG(ERROR) << "os::kill_process(): Failed call to OpenProcess"; return KILL_FAIL; } SharedHandle safe_process_handle(process_handle, ::CloseHandle); if (::TerminateProcess(safe_process_handle.get_handle(), 1) == 0) { LOG(ERROR) << "os::kill_process(): Failed call to TerminateProcess"; return KILL_FAIL; } return KILL_PASS; }
// Generate a `Process` object for the process associated with `pid`. If // process is not found, we return `None`; error is reserved for the case where // something went wrong. inline Result<Process> process(pid_t pid) { // Find process with pid. Result<PROCESSENTRY32> entry = process_entry(pid); if (entry.isError()) { return WindowsError(entry.error()); } else if (entry.isNone()) { return None(); } HANDLE process_handle = ::OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, false, pid); if (process_handle == INVALID_HANDLE_VALUE) { return WindowsError("os::process: Call to `OpenProcess` failed"); } SharedHandle safe_process_handle(process_handle, ::CloseHandle); // Get Windows Working set size (Resident set size in linux). PROCESS_MEMORY_COUNTERS proc_mem_counters; BOOL get_process_memory_info = ::GetProcessMemoryInfo( safe_process_handle.get(), &proc_mem_counters, sizeof(proc_mem_counters)); if (!get_process_memory_info) { return WindowsError("os::process: Call to `GetProcessMemoryInfo` failed"); } // Get session Id. pid_t session_id; BOOL process_id_to_session_id = ::ProcessIdToSessionId(pid, &session_id); if (!process_id_to_session_id) { return WindowsError("os::process: Call to `ProcessIdToSessionId` failed"); } // Get Process CPU time. FILETIME create_filetime, exit_filetime, kernel_filetime, user_filetime; BOOL get_process_times = ::GetProcessTimes( safe_process_handle.get(), &create_filetime, &exit_filetime, &kernel_filetime, &user_filetime); if (!get_process_times) { return WindowsError("os::process: Call to `GetProcessTimes` failed"); } // Get utime and stime. ULARGE_INTEGER lKernelTime, lUserTime; // In 100 nanoseconds. lKernelTime.HighPart = kernel_filetime.dwHighDateTime; lKernelTime.LowPart = kernel_filetime.dwLowDateTime; lUserTime.HighPart = user_filetime.dwHighDateTime; lUserTime.LowPart = user_filetime.dwLowDateTime; Try<Duration> utime = Nanoseconds(lKernelTime.QuadPart * 100); Try<Duration> stime = Nanoseconds(lUserTime.QuadPart * 100); return Process( pid, entry.get().th32ParentProcessID, // Parent process id. 0, // Group id. session_id, Bytes(proc_mem_counters.WorkingSetSize), utime.isSome() ? utime.get() : Option<Duration>::none(), stime.isSome() ? stime.get() : Option<Duration>::none(), entry.get().szExeFile, // Executable filename. false); // Is not zombie process. }