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
|
# frozen_string_literal: false
require 'test/unit'
require 'timeout'
require 'tempfile'
class TestSignal < Test::Unit::TestCase
def test_signal
begin
x = 0
oldtrap = Signal.trap(:INT) {|sig| x = 2 }
Process.kill :INT, Process.pid
10.times do
break if 2 == x
sleep 0.1
end
assert_equal 2, x
Signal.trap(:INT) { raise "Interrupt" }
assert_raise_with_message(RuntimeError, /Interrupt/) {
Process.kill :INT, Process.pid
sleep 0.1
}
ensure
Signal.trap :INT, oldtrap if oldtrap
end
end if Process.respond_to?(:kill)
def test_signal_process_group
bug4362 = '[ruby-dev:43169]'
assert_nothing_raised(bug4362) do
pid = Process.spawn(EnvUtil.rubybin, '-e', 'sleep 10', :pgroup => true)
Process.kill(:"-TERM", pid)
Process.waitpid(pid)
assert_equal(true, $?.signaled?)
assert_equal(Signal.list["TERM"], $?.termsig)
end
end if Process.respond_to?(:kill) and
Process.respond_to?(:pgroup) # for mswin32
def test_exit_action
if Signal.list[sig = "USR1"]
term = :TERM
else
sig = "INT"
term = :KILL
end
IO.popen([EnvUtil.rubybin, '-e', <<-"End"], 'r+') do |io|
Signal.trap(:#{sig}, "EXIT")
STDOUT.syswrite("a")
Thread.start { sleep(2) }
STDIN.sysread(4096)
End
pid = io.pid
io.sysread(1)
sleep 0.1
assert_nothing_raised("[ruby-dev:26128]") {
Process.kill(term, pid)
begin
Timeout.timeout(3) {
Process.waitpid pid
}
rescue Timeout::Error
if term
Process.kill(term, pid)
term = (:KILL if term != :KILL)
retry
end
raise
end
}
end
end if Process.respond_to?(:kill)
def test_invalid_signal_name
assert_raise(ArgumentError) { Process.kill(:XXXXXXXXXX, $$) }
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) { Process.kill("\u{30eb 30d3 30fc}", $$) }
end if Process.respond_to?(:kill)
def test_signal_exception
assert_raise(ArgumentError) { SignalException.new }
assert_raise(ArgumentError) { SignalException.new(-1) }
assert_raise(ArgumentError) { SignalException.new(:XXXXXXXXXX) }
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) { SignalException.new("\u{30eb 30d3 30fc}") }
Signal.list.each do |signm, signo|
next if signm == "EXIT"
assert_equal(SignalException.new(signm).signo, signo)
assert_equal(SignalException.new(signm.to_sym).signo, signo)
assert_equal(SignalException.new(signo).signo, signo)
end
end
def test_interrupt
assert_raise(Interrupt) { raise Interrupt.new }
end
def test_signal2
begin
x = false
oldtrap = Signal.trap(:INT) {|sig| x = true }
GC.start
assert_raise(ArgumentError) { Process.kill }
Timeout.timeout(10) do
x = false
Process.kill(SignalException.new(:INT).signo, $$)
sleep(0.01) until x
x = false
Process.kill("INT", $$)
sleep(0.01) until x
x = false
Process.kill("SIGINT", $$)
sleep(0.01) until x
x = false
o = Object.new
def o.to_str; "SIGINT"; end
Process.kill(o, $$)
sleep(0.01) until x
end
assert_raise(ArgumentError) { Process.kill(Object.new, $$) }
ensure
Signal.trap(:INT, oldtrap) if oldtrap
end
end if Process.respond_to?(:kill)
def test_trap
begin
oldtrap = Signal.trap(:INT) {|sig| }
assert_raise(ArgumentError) { Signal.trap }
assert_raise(SecurityError) do
s = proc {}.taint
Signal.trap(:INT, s)
end
# FIXME!
Signal.trap(:INT, nil)
Signal.trap(:INT, "")
Signal.trap(:INT, "SIG_IGN")
Signal.trap(:INT, "IGNORE")
Signal.trap(:INT, "SIG_DFL")
Signal.trap(:INT, "SYSTEM_DEFAULT")
Signal.trap(:INT, "EXIT")
Signal.trap(:INT, "xxxxxx")
Signal.trap(:INT, "xxxx")
Signal.trap(SignalException.new(:INT).signo, "SIG_DFL")
assert_raise(ArgumentError) { Signal.trap(-1, "xxxx") }
o = Object.new
def o.to_str; "SIGINT"; end
Signal.trap(o, "SIG_DFL")
assert_raise(ArgumentError) { Signal.trap("XXXXXXXXXX", "SIG_DFL") }
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) { Signal.trap("\u{30eb 30d3 30fc}", "SIG_DFL") }
ensure
Signal.trap(:INT, oldtrap) if oldtrap
end
end if Process.respond_to?(:kill)
%w"KILL STOP".each do |sig|
if Signal.list.key?(sig)
define_method("test_trap_uncatchable_#{sig}") do
assert_raise(Errno::EINVAL, "SIG#{sig} is not allowed to be caught") { Signal.trap(sig) {} }
end
end
end
def test_sigexit
assert_in_out_err([], 'Signal.trap(:EXIT) {print "OK"}', ["OK"])
assert_in_out_err([], 'Signal.trap("EXIT") {print "OK"}', ["OK"])
assert_in_out_err([], 'Signal.trap(:SIGEXIT) {print "OK"}', ["OK"])
assert_in_out_err([], 'Signal.trap("SIGEXIT") {print "OK"}', ["OK"])
assert_in_out_err([], 'Signal.trap(0) {print "OK"}', ["OK"])
end
def test_kill_immediately_before_termination
Signal.list[sig = "USR1"] or sig = "INT"
assert_in_out_err(["-e", <<-"end;"], "", %w"foo")
Signal.trap(:#{sig}) { STDOUT.syswrite("foo") }
Process.kill :#{sig}, $$
end;
end if Process.respond_to?(:kill)
def test_trap_system_default
assert_separately([], <<-End)
trap(:QUIT, "SYSTEM_DEFAULT")
assert_equal("SYSTEM_DEFAULT", trap(:QUIT, "DEFAULT"))
End
end if Signal.list.key?('QUIT')
def test_reserved_signal
assert_raise(ArgumentError) {
Signal.trap(:SEGV) {}
}
assert_raise(ArgumentError) {
Signal.trap(:BUS) {}
}
assert_raise(ArgumentError) {
Signal.trap(:ILL) {}
}
assert_raise(ArgumentError) {
Signal.trap(:FPE) {}
}
assert_raise(ArgumentError) {
Signal.trap(:VTALRM) {}
}
end
def test_signame
Signal.list.each do |name, num|
assert_equal(num, Signal.list[Signal.signame(num)], name)
end
assert_nil(Signal.signame(-1))
signums = Signal.list.invert
assert_nil(Signal.signame((1..1000).find {|num| !signums[num]}))
end
def test_signame_delivered
10.times do
IO.popen([EnvUtil.rubybin, "-e", <<EOS, :err => File::NULL]) do |child|
Signal.trap("INT") do |signo|
signame = Signal.signame(signo)
Marshal.dump(signame, STDOUT)
STDOUT.flush
exit 0
end
Process.kill("INT", $$)
sleep 1 # wait signal deliver
EOS
signame = Marshal.load(child)
assert_equal(signame, "INT")
end
end
end if Process.respond_to?(:kill)
def test_trap_puts
assert_in_out_err([], <<-INPUT, ["a"*10000], [])
Signal.trap(:INT) {
# for enable internal io mutex
STDOUT.sync = false
# larger than internal io buffer
print "a"*10000
}
Process.kill :INT, $$
sleep 0.1
INPUT
end if Process.respond_to?(:kill)
def test_hup_me
# [Bug #7951] [ruby-core:52864]
# This is MRI specific spec. ruby has no guarantee
# that signal will be deliverd synchronously.
# This ugly workaround was introduced to don't break
# compatibility against silly example codes.
assert_separately([], <<-RUBY)
trap(:HUP, "DEFAULT")
assert_raise(SignalException) {
Process.kill('HUP', Process.pid)
}
RUBY
bug8137 = '[ruby-dev:47182] [Bug #8137]'
assert_nothing_raised(bug8137) {
Timeout.timeout(1) {
Process.kill(0, Process.pid)
}
}
end if Process.respond_to?(:kill) and Signal.list.key?('HUP')
def test_ignored_interrupt
bug9820 = '[ruby-dev:48203] [Bug #9820]'
assert_separately(['-', bug9820], <<-'end;') # begin
bug = ARGV.shift
trap(:INT, "IGNORE")
assert_nothing_raised(SignalException, bug) do
Process.kill(:INT, $$)
end
end;
if trap = Signal.list['TRAP']
bug9820 = '[ruby-dev:48592] [Bug #9820]'
status = assert_in_out_err(['-e', 'Process.kill(:TRAP, $$)'])
assert_predicate(status, :signaled?, bug9820)
assert_equal(trap, status.termsig, bug9820)
end
if Signal.list['CONT']
bug9820 = '[ruby-dev:48606] [Bug #9820]'
assert_ruby_status(['-e', 'Process.kill(:CONT, $$)'])
end
end if Process.respond_to?(:kill)
def test_signal_list_dedupe_keys
a = Signal.list.keys.map(&:object_id).sort
b = Signal.list.keys.map(&:object_id).sort
assert_equal a, b
end
def test_self_stop
assert_ruby_status([], <<-'end;')
begin
fork{
sleep 1
Process.kill(:CONT, Process.ppid)
}
Process.kill(:STOP, Process.pid)
rescue NotImplementedError
# ok
end
end;
end
end
|