Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@52: This file copyright 2006 Chris Cannam. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "System.h" Chris@0: Chris@98: #ifdef __APPLE__ Chris@98: #include Chris@98: #include Chris@98: #else Chris@98: #ifndef _WIN32 Chris@98: #include Chris@98: #include Chris@98: #include Chris@98: #include Chris@98: #include Chris@98: #endif Chris@98: #endif Chris@98: Chris@98: #include Chris@98: Chris@0: #ifdef _WIN32 Chris@0: Chris@0: extern "C" { Chris@0: Chris@0: void gettimeofday(struct timeval *tv, void *tz) Chris@0: { Chris@0: union { Chris@0: long long ns100; Chris@0: FILETIME ft; Chris@0: } now; Chris@0: Chris@0: GetSystemTimeAsFileTime(&now.ft); Chris@0: tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL); Chris@0: tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL); Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: #endif Chris@98: Chris@98: ProcessStatus Chris@98: GetProcessStatus(int pid) Chris@98: { Chris@98: #ifdef __APPLE__ Chris@98: Chris@98: // See Chris@98: // http://tuvix.apple.com/documentation/Darwin/Reference/ManPages/man3/sysctl.3.html Chris@98: // http://developer.apple.com/qa/qa2001/qa1123.html Chris@98: Chris@98: int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0, 0 }; Chris@98: name[3] = pid; Chris@98: Chris@98: int err; Chris@98: size_t length = 0; Chris@98: Chris@98: if (sysctl(name, 4, 0, &length, 0, 0)) { Chris@98: perror("GetProcessStatus: sysctl failed"); Chris@98: return UnknownProcessStatus; Chris@98: } Chris@98: Chris@98: if (length > 0) return ProcessRunning; Chris@98: else return ProcessNotRunning; Chris@98: Chris@98: #elsif _WIN32 Chris@98: Chris@98: return UnknownProcessStatus; Chris@98: Chris@98: #else Chris@98: Chris@98: char filename[50]; Chris@98: struct stat statbuf; Chris@98: Chris@98: // Looking up the pid in /proc is worth a try on any POSIX system, Chris@98: // I guess -- it'll always compile and it won't return false Chris@98: // negatives if we do this first check: Chris@98: Chris@98: sprintf(filename, "/proc/%d", (int)getpid()); Chris@98: Chris@98: int err = stat(filename, &statbuf); Chris@98: Chris@98: if (err || !S_ISDIR(statbuf.st_mode)) { Chris@98: // If we can't even use it to tell whether we're running or Chris@98: // not, then clearly /proc is no use on this system. Chris@98: return UnknownProcessStatus; Chris@98: } Chris@98: Chris@98: sprintf(filename, "/proc/%d", (int)pid); Chris@98: Chris@98: err = stat(filename, &statbuf); Chris@98: Chris@98: if (!err) { Chris@98: if (S_ISDIR(statbuf.st_mode)) { Chris@98: return ProcessRunning; Chris@98: } else { Chris@98: return UnknownProcessStatus; Chris@98: } Chris@98: } else if (errno == ENOENT) { Chris@98: return ProcessNotRunning; Chris@98: } else { Chris@98: perror("stat failed"); Chris@98: return UnknownProcessStatus; Chris@98: } Chris@98: Chris@98: #endif Chris@98: } Chris@98: