config: Abolish use of ConfigParser's DEFAULT feature
[hippotat] / hippotatlib / __init__.py
CommitLineData
b0cfbfce 1# -*- python -*-
0256fc10
IJ
2#
3# Hippotat - Asinine IP Over HTTP program
4# hippotatlib/__init__.py - common library code
5#
6# Copyright 2017 Ian Jackson
7#
f85d143f 8# GPLv3+
0256fc10 9#
f85d143f
IJ
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
0256fc10 14#
f85d143f
IJ
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program, in the file GPLv3. If not,
22# see <http://www.gnu.org/licenses/>.
23
b0cfbfce 24
37ab4cdc
IJ
25import signal
26signal.signal(signal.SIGINT, signal.SIG_DFL)
27
1321ad5f 28import sys
cae50358 29import os
1321ad5f 30
b83d422a
IJ
31from zope.interface import implementer
32
040ff511
IJ
33import twisted
34from twisted.internet import reactor
1d023c89 35import twisted.internet.endpoints
8c3b6620
IJ
36import twisted.logger
37from twisted.logger import LogLevel
38import twisted.python.constants
39from twisted.python.constants import NamedConstant
b0cfbfce
IJ
40
41import ipaddress
42from ipaddress import AddressValueError
43
ae7c7784 44from optparse import OptionParser
5510890e 45import configparser
ae7c7784
IJ
46from configparser import ConfigParser
47from configparser import NoOptionError
48
c13ee6e6
IJ
49from functools import partial
50
ae7c7784 51import collections
84e763c7 52import time
8c3b6620 53import codecs
eedc8b30 54import traceback
ae7c7784 55
1321ad5f
IJ
56import re as regexp
57
5a37bac8 58import hippotatlib.slip as slip
1321ad5f 59
d579a048 60class DBG(twisted.python.constants.Names):
380ed56c 61 INIT = NamedConstant()
cae50358 62 CONFIG = NamedConstant()
d579a048 63 ROUTE = NamedConstant()
b68c0739 64 DROP = NamedConstant()
4a780703 65 OWNSOURCE = NamedConstant()
d579a048
IJ
66 FLOW = NamedConstant()
67 HTTP = NamedConstant()
380ed56c 68 TWISTED = NamedConstant()
d579a048 69 QUEUE = NamedConstant()
380ed56c 70 HTTP_CTRL = NamedConstant()
d579a048 71 QUEUE_CTRL = NamedConstant()
297b3ebf 72 HTTP_FULL = NamedConstant()
0accf0d3 73 CTRL_DUMP = NamedConstant()
380ed56c 74 SLIP_FULL = NamedConstant()
9acb0eca 75 DATA_COMPLETE = NamedConstant()
d579a048 76
b68c0739 77_hex_codec = codecs.getencoder('hex_codec')
8c3b6620 78
b83d422a
IJ
79#---------- logging ----------
80
81org_stderr = sys.stderr
82
8c3b6620
IJ
83log = twisted.logger.Logger()
84
2e68eb10
IJ
85debug_set = set()
86debug_def_detail = DBG.HTTP
3e35fc99 87
8c3b6620 88def log_debug(dflag, msg, idof=None, d=None):
3e35fc99 89 if dflag not in debug_set: return
e8fcf3b7 90 #print('---------------->',repr((dflag, msg, idof, d)), file=sys.stderr)
8c3b6620 91 if idof is not None:
e8ed0029 92 msg = '[%#x] %s' % (id(idof), msg)
8c3b6620 93 if d is not None:
9acb0eca
IJ
94 trunc = ''
95 if not DBG.DATA_COMPLETE in debug_set:
96 if len(d) > 64:
97 d = d[0:64]
98 trunc = '...'
b68c0739 99 d = _hex_codec(d)[0].decode('ascii')
9acb0eca 100 msg += ' ' + d + trunc
8c3b6620
IJ
101 log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg)
102
80e963a1
IJ
103def logevent_is_boringtwisted(event):
104 try:
105 if event.get('log_level') != LogLevel.info:
106 return False
107 dflag = event.get('dflag')
108 if dflag is False : return False
109 if dflag in debug_set: return False
110 if dflag is None and DBG.TWISTED in debug_set: return False
111 return True
112 except Exception:
02a201e1
IJ
113 print('EXCEPTION (IN BORINGTWISTED CHECK)',
114 traceback.format_exc(), file=org_stderr)
80e963a1
IJ
115 return False
116
b83d422a
IJ
117@implementer(twisted.logger.ILogFilterPredicate)
118class LogNotBoringTwisted:
119 def __call__(self, event):
80e963a1
IJ
120 return (
121 twisted.logger.PredicateResult.no
122 if logevent_is_boringtwisted(event) else
123 twisted.logger.PredicateResult.yes
124 )
b83d422a
IJ
125
126#---------- default config ----------
127
ca732796 128defcfg = '''
71f9ddb6 129[COMMON]
9e445690
IJ
130max_batch_down = 65536
131max_queue_time = 10
132target_requests_outstanding = 3
133http_timeout = 30
134http_timeout_grace = 5
135max_requests_outstanding = 6
136max_batch_up = 4000
137http_retry = 5
c7fb640e 138port = 80
8d374606 139vroutes = ''
d72f8360
IJ
140ifname_client = hippo%%d
141ifname_server = shippo%%d
ca732796
IJ
142
143#[server] or [<client>] overrides
d72f8360 144ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip,%(ifname)s %(rnets)s
ca732796 145
9e445690 146# relating to virtual network
ca732796 147mtu = 1500
ca732796 148
9e445690 149# addrs = 127.0.0.1 ::1
9e445690
IJ
150# url
151
152# relating to virtual network
8d374606
IJ
153vvnetwork = 172.24.230.192
154# vnetwork = <prefix>/<len>
c57e18a7 155# vaddr = <ipaddr>
8d374606 156# vrelay = <ipaddr>
9e445690 157
ca732796
IJ
158
159# [<client-ip4-or-ipv6-address>]
160# password = <password> # used by both, must match
161
c7fb640e 162[LIMIT]
9e445690
IJ
163max_batch_down = 262144
164max_queue_time = 121
165http_timeout = 121
166target_requests_outstanding = 10
ca732796
IJ
167'''
168
87a7c0c7 169# these need to be defined here so that they can be imported by import *
cae50358 170cfg = ConfigParser(strict=False)
ae7c7784
IJ
171optparser = OptionParser()
172
e4006ac4 173_mimetrans = bytes.maketrans(b'-'+slip.esc, slip.esc+b'-')
7b07f0b5
IJ
174def mime_translate(s):
175 # SLIP-encoded packets cannot contain ESC ESC.
176 # Swap `-' and ESC. The result cannot contain `--'
177 return s.translate(_mimetrans)
178
87a7c0c7 179class ConfigResults:
c7fb640e
IJ
180 def __init__(self):
181 pass
87a7c0c7
IJ
182 def __repr__(self):
183 return 'ConfigResults('+repr(self.__dict__)+')'
184
a8827d59 185def log_discard(packet, iface, saddr, daddr, why):
b68c0739 186 log_debug(DBG.DROP,
a8827d59 187 'discarded packet [%s] %s -> %s: %s' % (iface, saddr, daddr, why),
b68c0739 188 d=packet)
1321ad5f 189
b0cfbfce
IJ
190#---------- packet parsing ----------
191
192def packet_addrs(packet):
193 version = packet[0] >> 4
194 if version == 4:
195 addrlen = 4
196 saddroff = 3*4
197 factory = ipaddress.IPv4Address
198 elif version == 6:
199 addrlen = 16
200 saddroff = 2*4
201 factory = ipaddress.IPv6Address
202 else:
203 raise ValueError('unsupported IP version %d' % version)
204 saddr = factory(packet[ saddroff : saddroff + addrlen ])
205 daddr = factory(packet[ saddroff + addrlen : saddroff + addrlen*2 ])
206 return (saddr, daddr)
207
208#---------- address handling ----------
209
210def ipaddr(input):
211 try:
212 r = ipaddress.IPv4Address(input)
213 except AddressValueError:
214 r = ipaddress.IPv6Address(input)
215 return r
216
217def ipnetwork(input):
218 try:
219 r = ipaddress.IPv4Network(input)
220 except NetworkValueError:
221 r = ipaddress.IPv6Network(input)
222 return r
040ff511
IJ
223
224#---------- ipif (SLIP) subprocess ----------
225
a95cfeb2 226class SlipStreamDecoder():
db6ba584 227 def __init__(self, desc, on_packet):
040ff511 228 self._buffer = b''
a95cfeb2 229 self._on_packet = on_packet
db6ba584
IJ
230 self._desc = desc
231 self._log('__init__')
232
233 def _log(self, msg, **kwargs):
3297cac1 234 log_debug(DBG.SLIP_FULL, 'slip %s: %s' % (self._desc, msg), **kwargs)
a95cfeb2
IJ
235
236 def inputdata(self, data):
db6ba584 237 self._log('inputdata', d=data)
7fa9c132
IJ
238 data = self._buffer + data
239 self._buffer = b''
240 packets = slip.decode(data, True)
040ff511
IJ
241 self._buffer = packets.pop()
242 for packet in packets:
a95cfeb2 243 self._maybe_packet(packet)
54890d4d 244 self._log('bufremain', d=self._buffer)
a95cfeb2
IJ
245
246 def _maybe_packet(self, packet):
54890d4d 247 self._log('maybepacket', d=packet)
db6ba584
IJ
248 if len(packet):
249 self._on_packet(packet)
a95cfeb2 250
4f991c0c 251 def flush(self):
54890d4d 252 self._log('flush')
7fa9c132 253 data = self._buffer
a95cfeb2 254 self._buffer = b''
7fa9c132
IJ
255 packets = slip.decode(data)
256 assert(len(packets) == 1)
257 self._maybe_packet(packets[0])
4f991c0c 258
e4006ac4 259class _IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol):
4f991c0c
IJ
260 def __init__(self, router):
261 self._router = router
db6ba584 262 self._decoder = SlipStreamDecoder('ipif', self.slip_on_packet)
a95cfeb2
IJ
263 def connectionMade(self): pass
264 def outReceived(self, data):
265 self._decoder.inputdata(data)
266 def slip_on_packet(self, packet):
4f991c0c
IJ
267 (saddr, daddr) = packet_addrs(packet)
268 if saddr.is_link_local or daddr.is_link_local:
a8827d59 269 log_discard(packet, 'ipif', saddr, daddr, 'link-local')
4f991c0c
IJ
270 return
271 self._router(packet, saddr, daddr)
040ff511
IJ
272 def processEnded(self, status):
273 status.raiseException()
274
275def start_ipif(command, router):
040ff511
IJ
276 ipif = _IpifProcessProtocol(router)
277 reactor.spawnProcess(ipif,
278 '/bin/sh',['sh','-xc', command],
ff613365
IJ
279 childFDs={0:'w', 1:'r', 2:2},
280 env=None)
909e0ff3 281 return ipif
040ff511 282
909e0ff3 283def queue_inbound(ipif, packet):
15407d80 284 log_debug(DBG.FLOW, "queue_inbound", d=packet)
040ff511
IJ
285 ipif.transport.write(slip.delimiter)
286 ipif.transport.write(slip.encode(packet))
287 ipif.transport.write(slip.delimiter)
288
650a3251
IJ
289#---------- packet queue ----------
290
291class PacketQueue():
d579a048
IJ
292 def __init__(self, desc, max_queue_time):
293 self._desc = desc
8718b02c 294 assert(desc + '')
650a3251
IJ
295 self._max_queue_time = max_queue_time
296 self._pq = collections.deque() # packets
297
b68c0739 298 def _log(self, dflag, msg, **kwargs):
8c3b6620 299 log_debug(dflag, self._desc+' pq: '+msg, **kwargs)
d579a048 300
650a3251 301 def append(self, packet):
8c3b6620 302 self._log(DBG.QUEUE, 'append', d=packet)
650a3251
IJ
303 self._pq.append((time.monotonic(), packet))
304
305 def nonempty(self):
8c3b6620 306 self._log(DBG.QUEUE, 'nonempty ?')
650a3251
IJ
307 while True:
308 try: (queuetime, packet) = self._pq[0]
8c3b6620
IJ
309 except IndexError:
310 self._log(DBG.QUEUE, 'nonempty ? empty.')
311 return False
650a3251
IJ
312
313 age = time.monotonic() - queuetime
84e763c7 314 if age > self._max_queue_time:
650a3251 315 # strip old packets off the front
8c3b6620 316 self._log(DBG.QUEUE, 'dropping (old)', d=packet)
650a3251
IJ
317 self._pq.popleft()
318 continue
319
8c3b6620 320 self._log(DBG.QUEUE, 'nonempty ? nonempty.')
650a3251
IJ
321 return True
322
7b07f0b5
IJ
323 def process(self, sizequery, moredata, max_batch):
324 # sizequery() should return size of batch so far
325 # moredata(s) should add s to batch
8c3b6620 326 self._log(DBG.QUEUE, 'process...')
7b07f0b5
IJ
327 while True:
328 try: (dummy, packet) = self._pq[0]
8c3b6620
IJ
329 except IndexError:
330 self._log(DBG.QUEUE, 'process... empty')
331 break
332
333 self._log(DBG.QUEUE_CTRL, 'process... packet', d=packet)
7b07f0b5
IJ
334
335 encoded = slip.encode(packet)
336 sofar = sizequery()
337
8c3b6620
IJ
338 self._log(DBG.QUEUE_CTRL,
339 'process... (sofar=%d, max=%d) encoded' % (sofar, max_batch),
b68c0739 340 d=encoded)
8c3b6620 341
7b07f0b5
IJ
342 if sofar > 0:
343 if sofar + len(slip.delimiter) + len(encoded) > max_batch:
8c3b6620 344 self._log(DBG.QUEUE_CTRL, 'process... overflow')
7b07f0b5
IJ
345 break
346 moredata(slip.delimiter)
347
348 moredata(encoded)
84e763c7 349 self._pq.popleft()
ae7c7784
IJ
350
351#---------- error handling ----------
352
b68c0739
IJ
353_crashing = False
354
ae7c7784 355def crash(err):
b68c0739
IJ
356 global _crashing
357 _crashing = True
e8ed0029
IJ
358 print('========== CRASH ==========', err,
359 '===========================', file=sys.stderr)
ae7c7784
IJ
360 try: reactor.stop()
361 except twisted.internet.error.ReactorNotRunning: pass
362
363def crash_on_defer(defer):
364 defer.addErrback(lambda err: crash(err))
365
e4006ac4 366def crash_on_critical(event):
ae7c7784
IJ
367 if event.get('log_level') >= LogLevel.critical:
368 crash(twisted.logger.formatEvent(event))
369
87a7c0c7
IJ
370#---------- config processing ----------
371
c7fb640e
IJ
372def _cfg_process_putatives():
373 servers = { }
374 clients = { }
375 # maps from abstract object to canonical name for cs's
87a7c0c7 376
c7fb640e
IJ
377 def putative(cmap, abstract, canoncs):
378 try:
379 current_canoncs = cmap[abstract]
380 except KeyError:
381 pass
382 else:
383 assert(current_canoncs == canoncs)
384 cmap[abstract] = canoncs
385
386 server_pat = r'[-.0-9A-Za-z]+'
387 client_pat = r'[.:0-9a-f]+'
388 server_re = regexp.compile(server_pat)
389 serverclient_re = regexp.compile(server_pat + r' ' + client_pat)
88487243 390
c7fb640e 391 for cs in cfg.sections():
71f9ddb6
IJ
392 if cs == 'LIMIT' or cs == 'COMMON':
393 # plan A "[LIMIT]" or "[COMMON]"
c7fb640e 394 continue
88487243 395
c7fb640e
IJ
396 try:
397 # plan B "[<client>]" part 1
398 ci = ipaddr(cs)
399 except AddressValueError:
88487243 400
c7fb640e
IJ
401 if server_re.fullmatch(cs):
402 # plan C "[<servername>]"
403 putative(servers, cs, cs)
404 continue
405
406 if serverclient_re.fullmatch(cs):
407 # plan D "[<servername> <client>]" part 1
408 (pss,pcs) = cs.split(' ')
409
8d374606 410 if pcs == 'LIMIT':
c7fb640e
IJ
411 # plan E "[<servername> LIMIT]"
412 continue
413
414 try:
415 # plan D "[<servername> <client>]" part 2
416 ci = ipaddr(pc)
417 except AddressValueError:
418 # plan F "[<some thing we do not understand>]"
419 # well, we ignore this
420 print('warning: ignoring config section %s' % cs, file=sys.stderr)
421 continue
422
423 else: # no AddressValueError
424 # plan D "[<servername> <client]" part 3
425 putative(clients, ci, pcs)
426 putative(servers, pss, pss)
427 continue
428
429 else: # no AddressValueError
430 # plan B "[<client>" part 2
431 putative(clients, ci, cs)
432 continue
433
434 return (servers, clients)
435
62d13acc 436def cfg_process_general(c, ss):
300fe4ed 437 c.mtu = cfg1getint(ss, 'mtu')
c7fb640e
IJ
438
439def cfg_process_saddrs(c, ss):
440 class ServerAddr():
441 def __init__(self, port, addrspec):
442 self.port = port
443 # also self.addr
444 try:
445 self.addr = ipaddress.IPv4Address(addrspec)
446 self._endpointfactory = twisted.internet.endpoints.TCP4ServerEndpoint
447 self._inurl = b'%s'
448 except AddressValueError:
449 self.addr = ipaddress.IPv6Address(addrspec)
450 self._endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint
451 self._inurl = b'[%s]'
452 def make_endpoint(self):
3b69fba9
IJ
453 return self._endpointfactory(reactor, self.port,
454 interface= '%s' % self.addr)
c7fb640e
IJ
455 def url(self):
456 url = b'http://' + (self._inurl % str(self.addr).encode('ascii'))
457 if self.port != 80: url += b':%d' % self.port
458 url += b'/'
459 return url
3b69fba9
IJ
460 def __repr__(self):
461 return 'ServerAddr'+repr((self.port,self.addr))
c7fb640e 462
300fe4ed 463 c.port = cfg1getint(ss,'port')
c7fb640e 464 c.saddrs = [ ]
300fe4ed 465 for addrspec in cfg1get(ss, 'addrs').split():
c7fb640e
IJ
466 sa = ServerAddr(c.port, addrspec)
467 c.saddrs.append(sa)
468
469def cfg_process_vnetwork(c, ss):
300fe4ed 470 c.vnetwork = ipnetwork(cfg1get(ss,'vnetwork'))
c7f134ce
IJ
471 if c.vnetwork.num_addresses < 3 + 2:
472 raise ValueError('vnetwork needs at least 2^3 addresses')
88487243 473
8d374606 474def cfg_process_vaddr(c, ss):
88487243 475 try:
300fe4ed 476 c.vaddr = cfg1get(ss,'vaddr')
88487243 477 except NoOptionError:
8d374606 478 cfg_process_vnetwork(c, ss)
c7f134ce 479 c.vaddr = next(c.vnetwork.hosts())
88487243 480
c7fb640e
IJ
481def cfg_search_section(key,sections):
482 for section in sections:
483 if cfg.has_option(section, key):
484 return section
8d374606 485 raise NoOptionError(key, repr(sections))
c7fb640e 486
fa63bd93
IJ
487def cfg_get_raw(*args, **kwargs):
488 # for passing to cfg_search
489 return cfg.get(*args, raw=True, **kwargs)
490
c7fb640e
IJ
491def cfg_search(getter,key,sections):
492 section = cfg_search_section(key,sections)
493 return getter(section, key)
494
71f9ddb6
IJ
495def cfg1get(section,key, getter=cfg.get,**kwargs):
496 section = cfg_search_section(key,[section,'COMMON'])
497 return getter(section,key,**kwargs)
300fe4ed 498
71f9ddb6
IJ
499def cfg1getint(section,key, **kwargs):
500 return cfg1get(section,key, getter=cfg.getint,**kwargs);
300fe4ed 501
c7fb640e 502def cfg_process_client_limited(cc,ss,sections,key):
300fe4ed
IJ
503 val = cfg_search(cfg1getint, key, sections)
504 lim = cfg_search(cfg1getint, key, ['%s LIMIT' % ss, 'LIMIT'])
c7fb640e
IJ
505 cc.__dict__[key] = min(val,lim)
506
507def cfg_process_client_common(cc,ss,cs,ci):
508 # returns sections to search in, iff password is defined, otherwise None
509 cc.ci = ci
510
8d374606 511 sections = ['%s %s' % (ss,cs),
c7fb640e
IJ
512 cs,
513 ss,
71f9ddb6 514 'COMMON']
c7fb640e
IJ
515
516 try: pwsection = cfg_search_section('password', sections)
517 except NoOptionError: return None
88487243 518
300fe4ed 519 pw = cfg1get(pwsection, 'password')
c7f134ce 520 cc.password = pw.encode('utf-8')
88487243 521
c7fb640e
IJ
522 cfg_process_client_limited(cc,ss,sections,'target_requests_outstanding')
523 cfg_process_client_limited(cc,ss,sections,'http_timeout')
88487243 524
c7fb640e
IJ
525 return sections
526
8d374606 527def cfg_process_ipif(c, sections, varmap):
c7fb640e
IJ
528 for d, s in varmap:
529 try: v = getattr(c, s)
530 except AttributeError: continue
531 setattr(c, d, v)
532
c7f134ce 533 #print('CFGIPIF',repr((varmap, sections, c.__dict__)),file=sys.stderr)
8d374606 534
c7fb640e 535 section = cfg_search_section('ipif', sections)
300fe4ed 536 c.ipif_command = cfg1get(section,'ipif', vars=c.__dict__)
88487243 537
ae7c7784
IJ
538#---------- startup ----------
539
8c771381
IJ
540def log_debug_config(m):
541 if not DBG.CONFIG in debug_set: return
542 print('DBG.CONFIG:', m)
543
5510890e 544def common_startup(process_cfg):
c7fb640e
IJ
545 # calls process_cfg(putative_clients, putative_servers)
546
82302bac 547 # ConfigParser hates #-comments after values
c7fb640e 548 trailingcomments_re = regexp.compile(r'#.*')
82302bac 549 cfg.read_string(trailingcomments_re.sub('', defcfg))
cae50358
IJ
550 need_defcfg = True
551
552 def readconfig(pathname, mandatory=True):
553 def log(m, p=pathname):
554 if not DBG.CONFIG in debug_set: return
00ea5443 555 log_debug_config('%s: %s' % (m, p))
cae50358
IJ
556
557 try:
558 files = os.listdir(pathname)
559
560 except FileNotFoundError:
561 if mandatory: raise
562 log('skipped')
563 return
564
565 except NotADirectoryError:
566 cfg.read(pathname)
567 log('read file')
568 return
569
570 # is a directory
571 log('directory')
572 re = regexp.compile('[^-A-Za-z0-9_]')
2b13e1cc 573 for f in os.listdir(pathname):
cae50358
IJ
574 if re.search(f): continue
575 subpath = pathname + '/' + f
576 try:
577 os.stat(subpath)
578 except FileNotFoundError:
579 log('entry skipped', subpath)
580 continue
581 cfg.read(subpath)
582 log('entry read', subpath)
583
584 def oc_config(od,os, value, op):
585 nonlocal need_defcfg
586 need_defcfg = False
587 readconfig(value)
2e68eb10 588
26f04eff
IJ
589 def oc_extra_config(od,os, value, op):
590 readconfig(value)
591
7852bfc8
IJ
592 def read_defconfig():
593 readconfig('/etc/hippotat/config.d', False)
594 readconfig('/etc/hippotat/passwords.d', False)
595 readconfig('/etc/hippotat/master.cfg', False)
596
26f04eff
IJ
597 def oc_defconfig(od,os, value, op):
598 nonlocal need_defcfg
599 need_defcfg = False
600 read_defconfig(value)
601
9acb0eca
IJ
602 def dfs_less_detailed(dl):
603 return [df for df in DBG.iterconstants() if df <= dl]
604
605 def ds_default(od,os,dl,op):
2e68eb10 606 global debug_set
ff0fc3fa
IJ
607 debug_set.clear
608 debug_set |= set(dfs_less_detailed(debug_def_detail))
2e68eb10 609
9acb0eca 610 def ds_select(od,os, spec, op):
9acb0eca
IJ
611 for it in spec.split(','):
612
9acb0eca
IJ
613 if it.startswith('-'):
614 mutator = debug_set.discard
615 it = it[1:]
616 else:
617 mutator = debug_set.add
2cf75145
IJ
618
619 if it == '+':
620 dfs = DBG.iterconstants()
621
622 else:
623 if it.endswith('+'):
624 mapper = dfs_less_detailed
625 it = it[0:len(it)-1]
626 else:
627 mapper = lambda x: [x]
628
629 try:
630 dfspec = DBG.lookupByName(it)
631 except ValueError:
632 optparser.error('unknown debug flag %s in --debug-select' % it)
633
634 dfs = mapper(dfspec)
635
636 for df in dfs:
637 mutator(df)
9acb0eca
IJ
638
639 optparser.add_option('-D', '--debug',
2e68eb10
IJ
640 nargs=0,
641 action='callback',
9acb0eca
IJ
642 help='enable default debug (to stdout)',
643 callback= ds_default)
644
645 optparser.add_option('--debug-select',
646 nargs=1,
647 type='string',
2cf75145 648 metavar='[-]DFLAG[+]|[-]+,...',
9acb0eca 649 help=
2cf75145
IJ
650'''enable (`-': disable) each specified DFLAG;
651`+': do same for all "more interesting" DFLAGSs;
652just `+': all DFLAGs.
653 DFLAGS: ''' + ' '.join([df.name for df in DBG.iterconstants()]),
9acb0eca
IJ
654 action='callback',
655 callback= ds_select)
2e68eb10 656
cae50358
IJ
657 optparser.add_option('-c', '--config',
658 nargs=1,
659 type='string',
660 metavar='CONFIGFILE',
661 dest='configfile',
662 action='callback',
663 callback= oc_config)
664
26f04eff
IJ
665 optparser.add_option('--extra-config',
666 nargs=1,
667 type='string',
668 metavar='CONFIGFILE',
669 dest='configfile',
670 action='callback',
671 callback= oc_extra_config)
672
673 optparser.add_option('--default-config',
674 action='callback',
675 callback= oc_defconfig)
676
f022d67f
IJ
677 (opts, args) = optparser.parse_args()
678 if len(args): optparser.error('no non-option arguments please')
2e68eb10 679
cae50358 680 if need_defcfg:
7852bfc8 681 read_defconfig()
9acb0eca 682
c7fb640e 683 try:
8d374606 684 (pss, pcs) = _cfg_process_putatives()
1cc6968f 685 process_cfg(opts, pss, pcs)
5510890e
IJ
686 except (configparser.Error, ValueError):
687 traceback.print_exc(file=sys.stderr)
688 print('\nInvalid configuration, giving up.', file=sys.stderr)
689 sys.exit(12)
690
ff0fc3fa
IJ
691
692 #print('X', debug_set, file=sys.stderr)
2e68eb10 693
8c3b6620 694 log_formatter = twisted.logger.formatEventAsClassicLogText
389236df
IJ
695 stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter)
696 stderr_obs = twisted.logger.FileLogObserver(sys.stderr, log_formatter)
697 pred = twisted.logger.LogLevelFilterPredicate(LogLevel.error)
b83d422a 698 stdsomething_obs = twisted.logger.FilteringLogObserver(
389236df
IJ
699 stderr_obs, [pred], stdout_obs
700 )
ec2c9312
IJ
701 global file_log_observer
702 file_log_observer = twisted.logger.FilteringLogObserver(
b83d422a
IJ
703 stdsomething_obs, [LogNotBoringTwisted()]
704 )
705 #log_observer = stdsomething_obs
8c3b6620 706 twisted.logger.globalLogBeginner.beginLoggingTo(
ec2c9312 707 [ file_log_observer, crash_on_critical ]
8c3b6620 708 )
ae7c7784 709
ae7c7784 710def common_run():
b68c0739
IJ
711 log_debug(DBG.INIT, 'entering reactor')
712 if not _crashing: reactor.run()
2eecd19c 713 print('ENDED', file=sys.stderr)
207f5042 714 sys.exit(16)