-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
job_control.py
executable file
·225 lines (153 loc) · 3.83 KB
/
job_control.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
#!/usr/bin/env python3
"""
spec/stateful/job_control.py
"""
from __future__ import print_function
import signal
import sys
import time
import harness
from harness import register, stop_process__hack, expect_prompt
from test.spec_lib import log
# Hint from Stevens book
#
# http://lkml.iu.edu/hypermail/linux/kernel/1006.2/02460.html
# "TIOCSIG Generate a signal to processes in the
# current process group of the pty."
# Generated from C header file
TIOCSIG = 0x40045436
def ctrl_c(sh):
sh.sendcontrol('c')
#fcntl.ioctl(sh.child_fd, TIOCSIG, signal.SIGINT)
def ctrl_z(sh):
sh.sendcontrol('z')
#fcntl.ioctl(sh.child_fd, TIOCSIG, signal.SIGTSTP)
def expect_no_job(sh):
"""Helper function."""
if sh.shell_label == 'osh':
sh.expect('No job to put in the foreground')
elif sh.shell_label == 'dash':
sh.expect('.*fg: No current job')
elif sh.shell_label == 'bash':
sh.expect('.*fg: current: no such job.*')
else:
raise AssertionError()
@register()
def bug_1004(sh):
'fg twice should not result in fatal error (issue 1004)'
expect_prompt(sh)
sh.sendline('cat')
time.sleep(0.1)
if 0:
os.system('ls -l /proc/%s/fd' % os.getpid())
if 0:
ctrl_z(sh)
else:
stop_process__hack('cat')
sh.expect('.*Stopped.*')
#sh.expect("\r\n\\[PID \\d+\\] Stopped")
sh.sendline('') # needed for dash
expect_prompt(sh)
sh.sendline('fg')
if sh.shell_label == 'osh':
sh.expect(r'Continue PID \d+')
else:
sh.expect('cat')
# Ctrl-C to terminal
ctrl_c(sh)
expect_prompt(sh)
sh.sendline('fg')
expect_no_job(sh)
@register()
def bug_721(sh):
'Call fg twice after process exits (issue 721)'
# This test seems flaky under bash for some reason
expect_prompt(sh)
sh.sendline('cat')
time.sleep(0.1)
ctrl_c(sh)
expect_prompt(sh)
sh.sendline('fg')
expect_no_job(sh)
#sh.sendline('')
#expect_prompt(sh)
sh.sendline('fg')
expect_no_job(sh)
sh.sendline('')
expect_prompt(sh)
@register()
def bug_1005(sh):
'sleep 10 then Ctrl-Z then wait should not hang (issue 1005)'
expect_prompt(sh)
sh.sendline('sleep 10')
time.sleep(0.1)
if 1: # TODO: remove hack for OSH
stop_process__hack('sleep')
else:
ctrl_z(sh)
sh.expect(r'.*Stopped.*')
sh.sendline('wait')
sh.sendline('echo status=$?')
sh.expect('status=0')
@register(skip_shells=['dash'])
def bug_1005_wait_n(sh):
'sleep 10 then Ctrl-Z then wait -n should not hang'
expect_prompt(sh)
sh.sendline('sleep 10')
time.sleep(0.1)
if 1: # TODO: remove hack for OSH
stop_process__hack('sleep')
else:
ctrl_z(sh)
sh.expect(r'.*Stopped.*')
sh.sendline('wait -n')
sh.sendline('echo status=$?')
sh.expect('status=127')
@register()
def stopped_process(sh):
'Resuming a stopped process'
expect_prompt(sh)
sh.sendline('cat')
time.sleep(0.1) # seems necessary
if 0:
ctrl_z(sh)
else:
stop_process__hack('cat')
sh.expect('.*Stopped.*')
sh.sendline('') # needed for dash for some reason
expect_prompt(sh)
sh.sendline('fg')
if sh.shell_label == 'osh':
sh.expect(r'Continue PID \d+')
else:
sh.expect('cat')
ctrl_c(sh)
expect_prompt(sh)
sh.sendline('fg')
expect_no_job(sh)
@register()
def stopped_pipeline(sh):
'Resuming a stopped pipeline (issue 1087)'
expect_prompt(sh)
sh.sendline('sleep 10 | cat | cat')
time.sleep(0.1) # seems necessary
ctrl_z(sh)
# stop_process__hack doesn't work here
sh.expect('.*Stopped.*')
sh.sendline('') # needed for dash for some reason
expect_prompt(sh)
sh.sendline('fg')
if sh.shell_label == 'osh':
sh.expect(r'Continue PID \d+')
else:
sh.expect('cat')
ctrl_c(sh)
expect_prompt(sh)
sh.sendline('fg')
expect_no_job(sh)
if __name__ == '__main__':
try:
sys.exit(harness.main(sys.argv))
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)