# HG changeset patch # User Chris Cannam # Date 1146832491 0 # Node ID ce1d385f4f89d801af2e84f9fb628cde6d56701d # Parent 2b1a16e38d2dc7ab5431643f0fbc36f166de5c02 * Use kill(pid, 0) instead of /proc or sysctl blather for looking up pids * Add OpenProcess call for Win32 diff -r 2b1a16e38d2d -r ce1d385f4f89 base/System.cpp --- a/base/System.cpp Fri May 05 11:35:47 2006 +0000 +++ b/base/System.cpp Fri May 05 12:34:51 2006 +0000 @@ -15,17 +15,8 @@ #include "System.h" -#ifdef __APPLE__ -#include -#include -#else #ifndef _WIN32 -#include -#include -#include -#include -#include -#endif +#include #endif #include @@ -53,66 +44,24 @@ ProcessStatus GetProcessStatus(int pid) { -#ifdef __APPLE__ - - // See - // http://tuvix.apple.com/documentation/Darwin/Reference/ManPages/man3/sysctl.3.html - // http://developer.apple.com/qa/qa2001/qa1123.html - - int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0, 0 }; - name[3] = pid; - - int err; - size_t length = 0; - - if (sysctl(name, 4, 0, &length, 0, 0)) { - perror("GetProcessStatus: sysctl failed"); +#ifdef _WIN32 + DWORD handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); + if (!handle) { + return ProcessNotRunning; + } else { + CloseHandle(handle); + return ProcessRunning; + } +#else + if (kill(getpid(), 0) == 0) { + if (kill(pid, 0) == 0) { + return ProcessRunning; + } else { + return ProcessNotRunning; + } + } else { return UnknownProcessStatus; } - - if (length > 0) return ProcessRunning; - else return ProcessNotRunning; - -#elsif _WIN32 - - return UnknownProcessStatus; - -#else - - char filename[50]; - struct stat statbuf; - - // Looking up the pid in /proc is worth a try on any POSIX system, - // I guess -- it'll always compile and it won't return false - // negatives if we do this first check: - - sprintf(filename, "/proc/%d", (int)getpid()); - - int err = stat(filename, &statbuf); - - if (err || !S_ISDIR(statbuf.st_mode)) { - // If we can't even use it to tell whether we're running or - // not, then clearly /proc is no use on this system. - return UnknownProcessStatus; - } - - sprintf(filename, "/proc/%d", (int)pid); - - err = stat(filename, &statbuf); - - if (!err) { - if (S_ISDIR(statbuf.st_mode)) { - return ProcessRunning; - } else { - return UnknownProcessStatus; - } - } else if (errno == ENOENT) { - return ProcessNotRunning; - } else { - perror("stat failed"); - return UnknownProcessStatus; - } - #endif }