-
Notifications
You must be signed in to change notification settings - Fork 0
/
psig.py
executable file
·229 lines (214 loc) · 7.87 KB
/
psig.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Print human-readable signal info for specified linux process(es).
# By default this prints all signal info from /proc/<PID>/status.
#
# Author: Erik Weathers
from __future__ import print_function
import argparse
import collections
import errno
import glob
import re
import sys
def error(*objs):
print("ERROR: ", *objs, file=sys.stderr)
# Signal names for signal numbers.
#
# % kill -l
# 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
# 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
# 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
# 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
# 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
# 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
# 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
# 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
# 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
# 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
# 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
# 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
# 63) SIGRTMAX-1 64) SIGRTMAX
SIGNALS = {
1: 'SIGHUP',
2: 'SIGINT',
3: 'SIGQUIT',
4: 'SIGILL',
5: 'SIGTRAP',
6: 'SIGABRT',
7: 'SIGBUS',
8: 'SIGFPE',
9: 'SIGKILL',
10: 'SIGUSR1',
11: 'SIGSEGV',
12: 'SIGUSR2',
13: 'SIGPIPE',
14: 'SIGALRM',
15: 'SIGTERM',
16: 'SIGSTKFLT',
17: 'SIGCHLD',
18: 'SIGCONT',
19: 'SIGSTOP',
20: 'SIGTSTP',
21: 'SIGTTIN',
22: 'SIGTTOU',
23: 'SIGURG',
24: 'SIGXCPU',
25: 'SIGXFSZ',
26: 'SIGVTALRM',
27: 'SIGPROF',
28: 'SIGWINCH',
29: 'SIGIO',
30: 'SIGPWR',
31: 'SIGSYS',
34: 'SIGRTMIN',
35: 'SIGRTMIN+1',
36: 'SIGRTMIN+2',
37: 'SIGRTMIN+3',
38: 'SIGRTMIN+4',
39: 'SIGRTMIN+5',
40: 'SIGRTMIN+6',
41: 'SIGRTMIN+7',
42: 'SIGRTMIN+8',
43: 'SIGRTMIN+9',
44: 'SIGRTMIN+10',
45: 'SIGRTMIN+11',
46: 'SIGRTMIN+12',
47: 'SIGRTMIN+13',
48: 'SIGRTMIN+14',
49: 'SIGRTMIN+15',
50: 'SIGRTMAX-14',
51: 'SIGRTMAX-13',
52: 'SIGRTMAX-12',
53: 'SIGRTMAX-11',
54: 'SIGRTMAX-10',
55: 'SIGRTMAX-9',
56: 'SIGRTMAX-8',
57: 'SIGRTMAX-7',
58: 'SIGRTMAX-6',
59: 'SIGRTMAX-5',
60: 'SIGRTMAX-4',
61: 'SIGRTMAX-3',
62: 'SIGRTMAX-2',
63: 'SIGRTMAX-1',
64: 'SIGRTMAX'
}
def signal_bitmask_to_human_readable(bitmask_string):
signals = []
bitmask = int(bitmask_string, 16)
for signum, signame in SIGNALS.iteritems():
if (bitmask & (1 << (signum - 1))) != 0:
signals.append(signame)
return ','.join(signals)
# Return a dict of signal information scraped from the following
# lines from /proc/<PID>/status:
#
# SigQ: 2/58791
# SigPnd: 0000000000000000
# ShdPnd: 0000000000000000
# SigBlk: 0000000000000000
# SigIgn: 0000000000001000
# SigCgt: 00000001a0016623
def obtain_signal_info(pid):
signal_info = collections.OrderedDict()
with open("/proc/%d/status" % pid) as f:
pattern = r"^(?:Sig|ShdPnd).*$"
for line in re.findall(pattern, f.read(), flags=re.MULTILINE):
parts = line.split(":", 1)
if parts:
signal_info[parts[0]] = parts[1].strip()
return signal_info
SIGNAL_OPTIONS = {
'SigQ': 'signals_queued',
'SigPnd': 'pending_signals_for_thread',
'ShdPnd': 'pending_signals_for_whole_process',
'SigBlk': 'blocked_signals',
'SigIgn': 'ignored_signals',
'SigCgt': 'caught_signals'
}
def option_enabled(args, key):
if 'all_info' in vars(args):
return True
if SIGNAL_OPTIONS[key]:
if vars(args)[SIGNAL_OPTIONS[key]]:
return True
return False
OUTPUT_HEADERS = {
'SigQ': 'Signals Queued',
'SigPnd': 'Signals Pending',
'ShdPnd': 'Signals Pending (Shared)',
'SigBlk': 'Signals Blocked',
'SigIgn': 'Signals Ignored',
'SigCgt': 'Signals Caught'
}
def print_signal_info(args, pid, max_pid_length):
output_fmt = "[%%0%ss] %%s: %%s" % max_pid_length
for k,v in obtain_signal_info(pid).iteritems():
if option_enabled(args, k):
print(output_fmt % \
(pid,
OUTPUT_HEADERS[k],
v if k == 'SigQ' else signal_bitmask_to_human_readable(v)))
# Support hosts with a modified max PID number.
def get_max_pid_length():
pid_max = ''
with open('/proc/sys/kernel/pid_max') as f:
pid_max = f.read()
return len(pid_max)
def main(args):
max_pid_length = get_max_pid_length()
# Single PID?
if args.pid and not args.thread_info:
try:
print_signal_info(args, args.pid, max_pid_length)
except IOError as e:
if e.errno == 2:
sys.exc_clear()
error("PID %s does not exist" % args.pid)
sys.exit(1)
else:
raise
else:
# All Processes, all Threads, or all Threads of a single Process
try:
if args.all_pids:
glob_pattern = '/proc/[0-9]*'
elif args.all_threads:
glob_pattern = '/proc/[0-9]*/task/[0-9]*'
elif args.thread_info and args.pid:
glob_pattern = ('/proc/%d/task/[0-9]*' % args.pid)
else:
raise "Unexpected arguments specified, cannot proceed."
for proc_entry in glob.glob(glob_pattern):
pid = int(proc_entry.split('/')[-1])
print_signal_info(args, pid, max_pid_length)
except IOError as e:
if e.errno == errno.ENOENT:
# Swallow exception for the PID dir not existing, since it's likely
# some enumerated processes will die before we can print their info.
sys.exc_clear()
else:
raise
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Print human-readable signal info for specified process. By default all signal info is printed.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-p', '--pid', type=int, help='Process ID')
group.add_argument('-a', '--all-pids', action='store_true', help='Print signal info for all Processes')
group.add_argument('-T', '--all-threads', action='store_true', help='Print signal info for all Threads')
parser.add_argument('-t', '--thread-info', action='store_true', help='Print signal info for all Threads of the specified PID')
parser.add_argument('-A', '--all-info', action='store_true', default=True, help='Print all signal info for specified PID(s). Implies -b,-i,-c,-l,-P,-q.')
parser.add_argument('-b', '--blocked-signals', action='store_true', help='Print human-readable SigBlk from specified PID(s)')
parser.add_argument('-i', '--ignored-signals', action='store_true', help='Print human-readable SigIgn from specified PID(s)')
parser.add_argument('-c', '--caught-signals', action='store_true', help='Print human-readable SigCgt from specified PID(s)')
parser.add_argument('-l', '--pending-signals-for-thread', action='store_true', help='Print human-readable SigPnd from specified PID(s)')
parser.add_argument('-P', '--pending-signals-for-whole-process', action='store_true', help='Print human-readable ShdPnd from specified PID(s)')
parser.add_argument('-q', '--signals-queued', action='store_true', help='Print SigQ from specified PID(s)')
args = parser.parse_args()
if (args.blocked_signals or args.ignored_signals or args.caught_signals or args.pending_signals_for_thread or
args.pending_signals_for_whole_process or args.signals_queued):
vars(args).pop('all_info')
if (args.thread_info and not args.pid):
error("must specify a PID when using -t / --thread-info")
sys.exit(1)
main(args)