-
Notifications
You must be signed in to change notification settings - Fork 53
/
harshexit.py
167 lines (121 loc) · 4.33 KB
/
harshexit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# harshexit module -- Should be renamed, but I'm not sure what to.
# Provides these functions:
# portablekill: kill a function by pid
# harshexit: die, and do some things depending on the error code
# init_ostype: sets the module globals ostype and osrealtype
# used to get information about the system we're running on
import platform
import os
import sys
# needed for signal numbers
import signal
# needed for changing polling constants on the Nokia N800
import repy_constants
# Needed for kill_process; This will fail on non-windows systems
try:
import windows_api
except:
windows_api = None
# need for status retrieval
import statusstorage
# This prevents writes to the nanny's status information after we want to stop
statuslock = statusstorage.statuslock
ostype = None
osrealtype = None
# this indicates if we are exiting. Wrapping in a list to prevent needing a
# global (the purpose of this is described below)
statusexiting = [False]
class UnsupportedSystemException(Exception):
pass
def portablekill(pid):
global ostype
global osrealtype
if ostype == None:
init_ostype()
if ostype == 'Linux' or ostype == 'Darwin':
try:
os.kill(pid, signal.SIGTERM)
except:
pass
try:
os.kill(pid, signal.SIGKILL)
except:
pass
elif ostype == 'Windows':
# Use new api
windows_api.kill_process(pid)
else:
raise UnsupportedSystemException, "Unsupported system type: '"+osrealtype+"' (alias: "+ostype+")"
# exit all threads
def harshexit(val):
global ostype
global osrealtype
if ostype == None:
init_ostype()
# The problem is that there can be multiple calls to harshexit before we
# stop. For example, a signal (like we may send to kill) may trigger a
# call. As a result, we block all other status writers the first time this
# is called, but don't later on...
if not statusexiting[0]:
# do this once (now)
statusexiting[0] = True
# prevent concurrent writes to status info (acquire the lock to stop others,
# but do not block...
statuslock.acquire()
# we are stopped by the stop file watcher, not terminated through another
# mechanism
if val == 4:
# we were stopped by another thread. Let's exit
pass
# Special Termination signal to notify the NM of excessive threads
elif val == 56:
statusstorage.write_status("ThreadErr")
elif val == 44:
statusstorage.write_status("Stopped")
else:
# generic error, normal exit, or exitall in the user code...
statusstorage.write_status("Terminated")
# We intentionally do not release the lock. We don't want anyone else
# writing over our status information (we're killing them).
if ostype == 'Linux':
# The Nokia N800 refuses to exit on os._exit() by a thread. I'm going to
# signal our pid with SIGTERM (or SIGKILL if needed)
portablekill(os.getpid())
# os._exit(val)
elif ostype == 'Darwin':
os._exit(val)
elif ostype == 'Windows':
# stderr is not automatically flushed in Windows...
sys.stderr.flush()
os._exit(val)
else:
raise UnsupportedSystemException, "Unsupported system type: '"+osrealtype+"' (alias: "+ostype+")"
# Figure out the OS type
def init_ostype():
global ostype
global osrealtype
# figure out what sort of system we are...
osrealtype = platform.system()
# The Nokia N800 (and N900) uses the ARM architecture,
# and we change the constants on it to make disk checks happen less often
if platform.machine().startswith('armv'):
if osrealtype == 'Linux' or osrealtype == 'Darwin' or osrealtype == 'FreeBSD':
repy_constants.CPU_POLLING_FREQ_LINUX = repy_constants.CPU_POLLING_FREQ_WINCE;
repy_constants.RESOURCE_POLLING_FREQ_LINUX = repy_constants.RESOURCE_POLLING_FREQ_WINCE;
if osrealtype == 'Linux' or osrealtype == 'Windows' or osrealtype == 'Darwin':
ostype = osrealtype
return
# workaround for a Vista bug...
if osrealtype == 'Microsoft':
ostype = 'Windows'
return
if osrealtype == 'FreeBSD':
ostype = 'Linux'
return
if osrealtype.startswith('CYGWIN'):
# I do this because ps doesn't do memory info... They'll need to add
# pywin to their copy of cygwin... I wonder if I should detect its
# abscence and tell them (but continue)?
ostype = 'Windows'
return
ostype = 'Unknown'