-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
signals.py
executable file
·342 lines (236 loc) · 6.72 KB
/
signals.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
"""
signals.py
"""
from __future__ import print_function
import signal
import sys
import time
import harness
from harness import register, expect_prompt
#
# Test Cases
#
# TODO:
# - Fold code from demo/
# - sigwinch-bug.sh -- invokes $OSH with different snippets, then manual window resize
# - signal-during-read.sh -- bash_read and osh_read with manual kill -HUP $PID
# trap handler HUP
# - bug-858-trap.sh -- wait and kill -USR1 $PID
# trap handler USR1
# trap handler USR2
# - Fill out this TEST MATRIX.
#
# A. Which shell? osh, bash, dash, etc.
#
# B. What mode is it in?
#
# 1. Interactive (stdin is a terminal)
# 2. Non-interactive
#
# C. What is the main thread of the shell doing?
#
# 1. waiting for external process: sleep 1
# 2. wait builtin: sleep 5 & wait
# variants: wait -n: this matters when testing exit code
# 3. read builtin read
# variants: FIVE kinds, read -d, read -n, etc.
# 4. computation, e.g. fibonacci with $(( a + b ))
#
# if interactive:
# 5. keyboard input from terminal with select()
#
# Another way to categorize the main loop:
# 1. running script code
# 2. running trap code
# 3. running TAB completion plugin code
#
# D. What is it interrupted by?
#
# 1. SIGINT
# 2. SIGTSTP
# 3. SIGWINCH
# 4. SIGUSR1 -- doesn't this quit?
#
# if interactive:
# 1. SIGINT Ctrl-C from terminal (relies on signal distribution to child?)
# 2. SIGTSTP Ctrl-Z from terminal
#
# E. What is the signal state?
#
# 1. no trap handlers installed
# 2. trap 'echo X' SIGWINCH
# 3. trap 'echo X' SIGINT ?
@register()
def sigusr1_trapped_prompt(sh):
'Trapped SIGUSR1 while waiting at prompt (bug 1080)'
expect_prompt(sh)
sh.sendline("trap 'echo zzz-USR1' USR1")
expect_prompt(sh)
sh.kill(signal.SIGUSR1)
# bash and dash need another blank line, mksh doesn't
sh.sendline('')
sh.expect('zzz-USR1')
@register()
def sighup_trapped_wait(sh):
'trapped SIGHUP during wait builtin'
sh.sendline("trap 'echo HUP' HUP")
expect_prompt(sh)
sh.sendline('sleep 1 &')
# TODO: expect something like [1] 24370
sh.sendline('wait')
time.sleep(0.1)
sh.kill(signal.SIGHUP)
expect_prompt(sh)
sh.sendline('echo status=$?')
sh.expect('status=129')
@register()
def sigint_trapped_wait(sh):
'trapped SIGINT during wait builtin'
# This is different than Ctrl-C during wait builtin, because it's trapped!
sh.sendline("trap 'echo INT' INT")
sh.sendline('sleep 1 &')
sh.sendline('wait')
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGINT)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
@register()
def sigwinch_trapped_wait(sh):
'trapped SIGWINCH during wait builtin'
sh.sendline("trap 'echo WINCH' WINCH")
sh.sendline('sleep 1 &')
sh.sendline('wait')
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGWINCH)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=156')
@register()
def sigwinch_untrapped_wait(sh):
'untrapped SIGWINCH during wait builtin (issue 1067)'
sh.sendline('sleep 1 &')
sh.sendline('wait')
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGWINCH)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=0')
# dash and mksh don't have pipestatus
@register(skip_shells=['dash', 'mksh'])
def sigwinch_untrapped_wait_n(sh):
'untrapped SIGWINCH during wait -n'
sh.sendline('sleep 1 &')
sh.sendline('wait -n')
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGWINCH)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=0')
@register()
def sigwinch_untrapped_external(sh):
'untrapped SIGWINCH during external command'
sh.sendline('sleep 0.5') # slower than timeout
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGWINCH)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=0')
# dash doesn't have pipestatus
@register(skip_shells=['dash'])
def sigwinch_untrapped_pipeline(sh):
'untrapped SIGWINCH during pipeline'
sh.sendline('sleep 0.5 | echo x') # slower than timeout
time.sleep(0.1)
# simulate window size change
sh.kill(signal.SIGWINCH)
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo pipestatus=${PIPESTATUS[@]}')
sh.expect('pipestatus=0 0')
@register()
def t1(sh):
'Ctrl-C during external command'
sh.sendline('sleep 5')
time.sleep(0.1)
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
@register()
def t4(sh):
'Ctrl-C during pipeline'
sh.sendline('sleep 5 | cat')
time.sleep(0.1)
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
# TODO:
# - Expand this to call kinds of reads
# - note that mksh only has some of them
# - Expand to trapped and untrapped
# - Expand to Ctrl-C vs. SIGWINCH
@register()
def t2(sh):
'Ctrl-C during read builtin'
sh.sendline('read myvar')
time.sleep(0.1)
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
@register()
def c_wait(sh):
'Ctrl-C (untrapped) during wait builtin'
sh.sendline('sleep 5 &')
sh.sendline('wait')
time.sleep(0.1)
# TODO: actually send Ctrl-C through the terminal, not SIGINT?
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
@register()
def c_wait_n(sh):
'Ctrl-C (untrapped) during wait -n builtin'
sh.sendline('sleep 5 &')
sh.sendline('wait -n')
time.sleep(0.1)
# TODO: actually send Ctrl-C through the terminal, not SIGINT?
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
sh.expect('status=130')
@register()
def c_wait_line(sh):
'Ctrl-C (untrapped) cancels entire interactive line'
# the echo should be cancelled
sh.sendline('sleep 10 & wait; echo status=$?')
time.sleep(0.1)
# TODO: actually send Ctrl-C through the terminal, not SIGINT?
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo done=$?')
sh.expect('done=130')
@register()
def t5(sh):
'Ctrl-C during Command Sub (issue 467)'
sh.sendline('`sleep 5`')
time.sleep(0.1)
sh.sendintr() # SIGINT
sh.expect(r'.*\$') # expect prompt
sh.sendline('echo status=$?')
# TODO: This should be status 130 like bash
sh.expect('status=130')
if __name__ == '__main__':
try:
sys.exit(harness.main(sys.argv))
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)