Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1 | """ |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 2 | Test suite for socketserver. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3 | """ |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 4 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 5 | import contextlib |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 6 | import imp |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 7 | import os |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 8 | import select |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 9 | import signal |
| 10 | import socket |
| 11 | import tempfile |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 12 | import unittest |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 13 | import socketserver |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 15 | import test.support |
Antoine Pitrou | c00d4b4 | 2009-10-27 21:32:38 +0000 | [diff] [blame] | 16 | from test.support import reap_children, reap_threads, verbose |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 17 | try: |
| 18 | import threading |
| 19 | except ImportError: |
| 20 | threading = None |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 21 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | test.support.requires("network") |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 24 | TEST_STR = b"hello world\n" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 25 | HOST = test.support.HOST |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 26 | |
| 27 | HAVE_UNIX_SOCKETS = hasattr(socket, "AF_UNIX") |
| 28 | HAVE_FORKING = hasattr(os, "fork") and os.name != "os2" |
| 29 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | def signal_alarm(n): |
| 31 | """Call signal.alarm when it exists (i.e. not on Windows).""" |
| 32 | if hasattr(signal, 'alarm'): |
| 33 | signal.alarm(n) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 34 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 35 | def receive(sock, n, timeout=20): |
| 36 | r, w, x = select.select([sock], [], [], timeout) |
| 37 | if sock in r: |
| 38 | return sock.recv(n) |
| 39 | else: |
| 40 | raise RuntimeError("timed out on %r" % (sock,)) |
| 41 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 42 | if HAVE_UNIX_SOCKETS: |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 43 | class ForkingUnixStreamServer(socketserver.ForkingMixIn, |
| 44 | socketserver.UnixStreamServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 45 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 46 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 47 | class ForkingUnixDatagramServer(socketserver.ForkingMixIn, |
| 48 | socketserver.UnixDatagramServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 49 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 50 | |
| 51 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 52 | @contextlib.contextmanager |
| 53 | def simple_subprocess(testcase): |
| 54 | pid = os.fork() |
| 55 | if pid == 0: |
| 56 | # Don't throw an exception; it would be caught by the test harness. |
| 57 | os._exit(72) |
| 58 | yield None |
| 59 | pid2, status = os.waitpid(pid, 0) |
| 60 | testcase.assertEquals(pid2, pid) |
| 61 | testcase.assertEquals(72 << 8, status) |
| 62 | |
| 63 | |
Victor Stinner | 5f22e72 | 2010-05-26 17:33:03 +0000 | [diff] [blame] | 64 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 65 | class SocketServerTest(unittest.TestCase): |
| 66 | """Test all socket servers.""" |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 67 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 68 | def setUp(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 69 | signal_alarm(20) # Kill deadlocks after 20 seconds. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 70 | self.port_seed = 0 |
| 71 | self.test_files = [] |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 72 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 73 | def tearDown(self): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 74 | signal_alarm(0) # Didn't deadlock. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 75 | reap_children() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 77 | for fn in self.test_files: |
| 78 | try: |
| 79 | os.remove(fn) |
| 80 | except os.error: |
| 81 | pass |
| 82 | self.test_files[:] = [] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 83 | |
| 84 | def pickaddr(self, proto): |
| 85 | if proto == socket.AF_INET: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 86 | return (HOST, 0) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 87 | else: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 88 | # XXX: We need a way to tell AF_UNIX to pick its own name |
| 89 | # like AF_INET provides port==0. |
| 90 | dir = None |
| 91 | if os.name == 'os2': |
| 92 | dir = '\socket' |
| 93 | fn = tempfile.mktemp(prefix='unix_socket.', dir=dir) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 94 | if os.name == 'os2': |
| 95 | # AF_UNIX socket names on OS/2 require a specific prefix |
| 96 | # which can't include a drive letter and must also use |
| 97 | # backslashes as directory separators |
| 98 | if fn[1] == ':': |
| 99 | fn = fn[2:] |
| 100 | if fn[0] in (os.sep, os.altsep): |
| 101 | fn = fn[1:] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 102 | if os.sep == '/': |
| 103 | fn = fn.replace(os.sep, os.altsep) |
| 104 | else: |
| 105 | fn = fn.replace(os.altsep, os.sep) |
| 106 | self.test_files.append(fn) |
| 107 | return fn |
| 108 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 109 | def make_server(self, addr, svrcls, hdlrbase): |
| 110 | class MyServer(svrcls): |
| 111 | def handle_error(self, request, client_address): |
| 112 | self.close_request(request) |
| 113 | self.server_close() |
| 114 | raise |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 115 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 116 | class MyHandler(hdlrbase): |
| 117 | def handle(self): |
| 118 | line = self.rfile.readline() |
| 119 | self.wfile.write(line) |
| 120 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 121 | if verbose: print("creating server") |
| 122 | server = MyServer(addr, MyHandler) |
| 123 | self.assertEquals(server.server_address, server.socket.getsockname()) |
| 124 | return server |
| 125 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 126 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | c00d4b4 | 2009-10-27 21:32:38 +0000 | [diff] [blame] | 127 | @reap_threads |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 128 | def run_server(self, svrcls, hdlrbase, testfunc): |
| 129 | server = self.make_server(self.pickaddr(svrcls.address_family), |
| 130 | svrcls, hdlrbase) |
| 131 | # We had the OS pick a port, so pull the real address out of |
| 132 | # the server. |
| 133 | addr = server.server_address |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 134 | if verbose: |
| 135 | print("ADDR =", addr) |
| 136 | print("CLASS =", svrcls) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 137 | |
| 138 | t = threading.Thread( |
| 139 | name='%s serving' % svrcls, |
| 140 | target=server.serve_forever, |
| 141 | # Short poll interval to make the test finish quickly. |
| 142 | # Time between requests is short enough that we won't wake |
| 143 | # up spuriously too many times. |
| 144 | kwargs={'poll_interval':0.01}) |
Benjamin Peterson | 7275370 | 2008-08-18 18:09:21 +0000 | [diff] [blame] | 145 | t.daemon = True # In case this function raises. |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 146 | t.start() |
| 147 | if verbose: print("server running") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 148 | for i in range(3): |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 149 | if verbose: print("test client", i) |
| 150 | testfunc(svrcls.address_family, addr) |
| 151 | if verbose: print("waiting for server") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 152 | server.shutdown() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 153 | t.join() |
| 154 | if verbose: print("done") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 155 | |
| 156 | def stream_examine(self, proto, addr): |
| 157 | s = socket.socket(proto, socket.SOCK_STREAM) |
| 158 | s.connect(addr) |
| 159 | s.sendall(TEST_STR) |
| 160 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 161 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 162 | data = receive(s, 100) |
| 163 | buf += data |
| 164 | self.assertEquals(buf, TEST_STR) |
| 165 | s.close() |
| 166 | |
| 167 | def dgram_examine(self, proto, addr): |
| 168 | s = socket.socket(proto, socket.SOCK_DGRAM) |
| 169 | s.sendto(TEST_STR, addr) |
| 170 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 171 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 172 | data = receive(s, 100) |
| 173 | buf += data |
| 174 | self.assertEquals(buf, TEST_STR) |
| 175 | s.close() |
| 176 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 177 | def test_TCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 178 | self.run_server(socketserver.TCPServer, |
| 179 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 180 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 181 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 182 | def test_ThreadingTCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 183 | self.run_server(socketserver.ThreadingTCPServer, |
| 184 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 185 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 186 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 187 | if HAVE_FORKING: |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 188 | def test_ForkingTCPServer(self): |
| 189 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 190 | self.run_server(socketserver.ForkingTCPServer, |
| 191 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 192 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 193 | |
| 194 | if HAVE_UNIX_SOCKETS: |
| 195 | def test_UnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 196 | self.run_server(socketserver.UnixStreamServer, |
| 197 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 198 | self.stream_examine) |
| 199 | |
| 200 | def test_ThreadingUnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 201 | self.run_server(socketserver.ThreadingUnixStreamServer, |
| 202 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 203 | self.stream_examine) |
| 204 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 205 | if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 206 | def test_ForkingUnixStreamServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 207 | with simple_subprocess(self): |
| 208 | self.run_server(ForkingUnixStreamServer, |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 209 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 210 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 211 | |
| 212 | def test_UDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 213 | self.run_server(socketserver.UDPServer, |
| 214 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 215 | self.dgram_examine) |
| 216 | |
| 217 | def test_ThreadingUDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 218 | self.run_server(socketserver.ThreadingUDPServer, |
| 219 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 220 | self.dgram_examine) |
| 221 | |
| 222 | if HAVE_FORKING: |
| 223 | def test_ForkingUDPServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 224 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 225 | self.run_server(socketserver.ForkingUDPServer, |
| 226 | socketserver.DatagramRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 227 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 228 | |
| 229 | # Alas, on Linux (at least) recvfrom() doesn't return a meaningful |
| 230 | # client address so this cannot work: |
| 231 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 232 | # if HAVE_UNIX_SOCKETS: |
| 233 | # def test_UnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 234 | # self.run_server(socketserver.UnixDatagramServer, |
| 235 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 236 | # self.dgram_examine) |
| 237 | # |
| 238 | # def test_ThreadingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 239 | # self.run_server(socketserver.ThreadingUnixDatagramServer, |
| 240 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 241 | # self.dgram_examine) |
| 242 | # |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 243 | # if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 244 | # def test_ForkingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 245 | # self.run_server(socketserver.ForkingUnixDatagramServer, |
| 246 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 247 | # self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | 3bcba8e | 2010-04-25 22:01:43 +0000 | [diff] [blame] | 249 | @reap_threads |
| 250 | def test_shutdown(self): |
| 251 | # Issue #2302: shutdown() should always succeed in making an |
| 252 | # other thread leave serve_forever(). |
| 253 | class MyServer(socketserver.TCPServer): |
| 254 | pass |
| 255 | |
| 256 | class MyHandler(socketserver.StreamRequestHandler): |
| 257 | pass |
| 258 | |
| 259 | threads = [] |
| 260 | for i in range(20): |
| 261 | s = MyServer((HOST, 0), MyHandler) |
| 262 | t = threading.Thread( |
| 263 | name='MyServer serving', |
| 264 | target=s.serve_forever, |
| 265 | kwargs={'poll_interval':0.01}) |
| 266 | t.daemon = True # In case this function raises. |
| 267 | threads.append((t, s)) |
| 268 | for t, s in threads: |
| 269 | t.start() |
| 270 | s.shutdown() |
| 271 | for t, s in threads: |
| 272 | t.join() |
| 273 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 274 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 275 | def test_main(): |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 276 | if imp.lock_held(): |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 277 | # If the import lock is held, the threads will hang |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 278 | raise unittest.SkipTest("can't run when import lock is held") |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 279 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 280 | test.support.run_unittest(SocketServerTest) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 281 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 282 | if __name__ == "__main__": |
| 283 | test_main() |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 284 | signal_alarm(3) # Shutdown shouldn't take more than 3 seconds. |