| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2021 Igara Studio S.A. |
| 3 | // Copyright (c) 2015-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "base/process.h" |
| 13 | |
| 14 | #if LAF_WINDOWS |
| 15 | #include <windows.h> |
| 16 | #else |
| 17 | #include <signal.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | #endif |
| 21 | |
| 22 | namespace base { |
| 23 | |
| 24 | #if LAF_WINDOWS |
| 25 | |
| 26 | pid get_current_process_id() |
| 27 | { |
| 28 | return (pid)GetCurrentProcessId(); |
| 29 | } |
| 30 | |
| 31 | bool is_process_running(pid pid) |
| 32 | { |
| 33 | bool running = false; |
| 34 | |
| 35 | HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); |
| 36 | if (handle) { |
| 37 | DWORD exitCode = 0; |
| 38 | if (GetExitCodeProcess(handle, &exitCode)) { |
| 39 | running = (exitCode == STILL_ACTIVE); |
| 40 | } |
| 41 | CloseHandle(handle); |
| 42 | } |
| 43 | |
| 44 | return running; |
| 45 | } |
| 46 | |
| 47 | #else // !LAF_WINDOWS |
| 48 | |
| 49 | pid get_current_process_id() |
| 50 | { |
| 51 | return (pid)getpid(); |
| 52 | } |
| 53 | |
| 54 | bool is_process_running(pid pid) |
| 55 | { |
| 56 | return (kill(pid, 0) == 0); |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | } // namespace base |
| 62 | |