Chris@49
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@52
|
4 Sonic Visualiser
|
Chris@52
|
5 An audio file viewer and annotation editor.
|
Chris@52
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@52
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@52
|
9 This program is free software; you can redistribute it and/or
|
Chris@52
|
10 modify it under the terms of the GNU General Public License as
|
Chris@52
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
12 License, or (at your option) any later version. See the file
|
Chris@52
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "System.h"
|
Chris@0
|
17
|
Chris@98
|
18 #ifdef __APPLE__
|
Chris@98
|
19 #include <sys/types.h>
|
Chris@98
|
20 #include <sys/sysctl.h>
|
Chris@98
|
21 #else
|
Chris@98
|
22 #ifndef _WIN32
|
Chris@98
|
23 #include <unistd.h>
|
Chris@98
|
24 #include <cstdio>
|
Chris@98
|
25 #include <sys/types.h>
|
Chris@98
|
26 #include <sys/stat.h>
|
Chris@98
|
27 #include <errno.h>
|
Chris@98
|
28 #endif
|
Chris@98
|
29 #endif
|
Chris@98
|
30
|
Chris@98
|
31 #include <iostream>
|
Chris@98
|
32
|
Chris@0
|
33 #ifdef _WIN32
|
Chris@0
|
34
|
Chris@0
|
35 extern "C" {
|
Chris@0
|
36
|
Chris@0
|
37 void gettimeofday(struct timeval *tv, void *tz)
|
Chris@0
|
38 {
|
Chris@0
|
39 union {
|
Chris@0
|
40 long long ns100;
|
Chris@0
|
41 FILETIME ft;
|
Chris@0
|
42 } now;
|
Chris@0
|
43
|
Chris@0
|
44 GetSystemTimeAsFileTime(&now.ft);
|
Chris@0
|
45 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
|
Chris@0
|
46 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 #endif
|
Chris@98
|
52
|
Chris@98
|
53 ProcessStatus
|
Chris@98
|
54 GetProcessStatus(int pid)
|
Chris@98
|
55 {
|
Chris@98
|
56 #ifdef __APPLE__
|
Chris@98
|
57
|
Chris@98
|
58 // See
|
Chris@98
|
59 // http://tuvix.apple.com/documentation/Darwin/Reference/ManPages/man3/sysctl.3.html
|
Chris@98
|
60 // http://developer.apple.com/qa/qa2001/qa1123.html
|
Chris@98
|
61
|
Chris@98
|
62 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0, 0 };
|
Chris@98
|
63 name[3] = pid;
|
Chris@98
|
64
|
Chris@98
|
65 int err;
|
Chris@98
|
66 size_t length = 0;
|
Chris@98
|
67
|
Chris@98
|
68 if (sysctl(name, 4, 0, &length, 0, 0)) {
|
Chris@98
|
69 perror("GetProcessStatus: sysctl failed");
|
Chris@98
|
70 return UnknownProcessStatus;
|
Chris@98
|
71 }
|
Chris@98
|
72
|
Chris@98
|
73 if (length > 0) return ProcessRunning;
|
Chris@98
|
74 else return ProcessNotRunning;
|
Chris@98
|
75
|
Chris@98
|
76 #elsif _WIN32
|
Chris@98
|
77
|
Chris@98
|
78 return UnknownProcessStatus;
|
Chris@98
|
79
|
Chris@98
|
80 #else
|
Chris@98
|
81
|
Chris@98
|
82 char filename[50];
|
Chris@98
|
83 struct stat statbuf;
|
Chris@98
|
84
|
Chris@98
|
85 // Looking up the pid in /proc is worth a try on any POSIX system,
|
Chris@98
|
86 // I guess -- it'll always compile and it won't return false
|
Chris@98
|
87 // negatives if we do this first check:
|
Chris@98
|
88
|
Chris@98
|
89 sprintf(filename, "/proc/%d", (int)getpid());
|
Chris@98
|
90
|
Chris@98
|
91 int err = stat(filename, &statbuf);
|
Chris@98
|
92
|
Chris@98
|
93 if (err || !S_ISDIR(statbuf.st_mode)) {
|
Chris@98
|
94 // If we can't even use it to tell whether we're running or
|
Chris@98
|
95 // not, then clearly /proc is no use on this system.
|
Chris@98
|
96 return UnknownProcessStatus;
|
Chris@98
|
97 }
|
Chris@98
|
98
|
Chris@98
|
99 sprintf(filename, "/proc/%d", (int)pid);
|
Chris@98
|
100
|
Chris@98
|
101 err = stat(filename, &statbuf);
|
Chris@98
|
102
|
Chris@98
|
103 if (!err) {
|
Chris@98
|
104 if (S_ISDIR(statbuf.st_mode)) {
|
Chris@98
|
105 return ProcessRunning;
|
Chris@98
|
106 } else {
|
Chris@98
|
107 return UnknownProcessStatus;
|
Chris@98
|
108 }
|
Chris@98
|
109 } else if (errno == ENOENT) {
|
Chris@98
|
110 return ProcessNotRunning;
|
Chris@98
|
111 } else {
|
Chris@98
|
112 perror("stat failed");
|
Chris@98
|
113 return UnknownProcessStatus;
|
Chris@98
|
114 }
|
Chris@98
|
115
|
Chris@98
|
116 #endif
|
Chris@98
|
117 }
|
Chris@98
|
118
|