
In order to determine what to do about this module it is quite useful to provide for a way to run it as its own main program (and interact with it from another telnet session). This adds a simple `main` function and uses it. This can then be used like: $ python oslo_service/eventlet_backdoor.py --backdoor_port 8000 Then from another terminal: $ telnet localhost 8000 Change-Id: I8c2b93dfad328929e1a106c5883903fe0bfb188a
167 lines
4.7 KiB
Python
167 lines
4.7 KiB
Python
# Copyright (c) 2012 OpenStack Foundation.
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
# All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from __future__ import print_function
|
|
|
|
import errno
|
|
import gc
|
|
import logging
|
|
import os
|
|
import pprint
|
|
import socket
|
|
import sys
|
|
import traceback
|
|
|
|
import eventlet.backdoor
|
|
import greenlet
|
|
|
|
from oslo_service._i18n import _LI
|
|
from oslo_service import _options
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class EventletBackdoorConfigValueError(Exception):
|
|
def __init__(self, port_range, help_msg, ex):
|
|
msg = ('Invalid backdoor_port configuration %(range)s: %(ex)s. '
|
|
'%(help)s' %
|
|
{'range': port_range, 'ex': ex, 'help': help_msg})
|
|
super(EventletBackdoorConfigValueError, self).__init__(msg)
|
|
self.port_range = port_range
|
|
|
|
|
|
def _dont_use_this():
|
|
print("Don't use this, just disconnect instead")
|
|
|
|
|
|
def _find_objects(t):
|
|
return [o for o in gc.get_objects() if isinstance(o, t)]
|
|
|
|
|
|
def _print_greenthreads():
|
|
for i, gt in enumerate(_find_objects(greenlet.greenlet)):
|
|
print(i, gt)
|
|
traceback.print_stack(gt.gr_frame)
|
|
print()
|
|
|
|
|
|
def _print_nativethreads():
|
|
for threadId, stack in sys._current_frames().items():
|
|
print(threadId)
|
|
traceback.print_stack(stack)
|
|
print()
|
|
|
|
|
|
def _parse_port_range(port_range):
|
|
if ':' not in port_range:
|
|
start, end = port_range, port_range
|
|
else:
|
|
start, end = port_range.split(':', 1)
|
|
try:
|
|
start, end = int(start), int(end)
|
|
if end < start:
|
|
raise ValueError
|
|
return start, end
|
|
except ValueError as ex:
|
|
raise EventletBackdoorConfigValueError(
|
|
port_range, ex, _options.help_for_backdoor_port)
|
|
|
|
|
|
def _listen(host, start_port, end_port, listen_func):
|
|
try_port = start_port
|
|
while True:
|
|
try:
|
|
return listen_func((host, try_port))
|
|
except socket.error as exc:
|
|
if (exc.errno != errno.EADDRINUSE or
|
|
try_port >= end_port):
|
|
raise
|
|
try_port += 1
|
|
|
|
|
|
def _initialize_if_enabled(conf):
|
|
conf.register_opts(_options.eventlet_backdoor_opts)
|
|
backdoor_locals = {
|
|
'exit': _dont_use_this, # So we don't exit the entire process
|
|
'quit': _dont_use_this, # So we don't exit the entire process
|
|
'fo': _find_objects,
|
|
'pgt': _print_greenthreads,
|
|
'pnt': _print_nativethreads,
|
|
}
|
|
|
|
if conf.backdoor_port is None:
|
|
return None
|
|
|
|
start_port, end_port = _parse_port_range(str(conf.backdoor_port))
|
|
|
|
# NOTE(johannes): The standard sys.displayhook will print the value of
|
|
# the last expression and set it to __builtin__._, which overwrites
|
|
# the __builtin__._ that gettext sets. Let's switch to using pprint
|
|
# since it won't interact poorly with gettext, and it's easier to
|
|
# read the output too.
|
|
def displayhook(val):
|
|
if val is not None:
|
|
pprint.pprint(val)
|
|
sys.displayhook = displayhook
|
|
|
|
sock = _listen('localhost', start_port, end_port, eventlet.listen)
|
|
|
|
# In the case of backdoor port being zero, a port number is assigned by
|
|
# listen(). In any case, pull the port number out here.
|
|
port = sock.getsockname()[1]
|
|
LOG.info(
|
|
_LI('Eventlet backdoor listening on %(port)s for process %(pid)d'),
|
|
{'port': port, 'pid': os.getpid()}
|
|
)
|
|
thread = eventlet.spawn(eventlet.backdoor.backdoor_server, sock,
|
|
locals=backdoor_locals)
|
|
return (port, thread)
|
|
|
|
|
|
def initialize_if_enabled(conf):
|
|
port_thread = _initialize_if_enabled(conf)
|
|
if not port_thread:
|
|
return None
|
|
else:
|
|
port, _thread = port_thread
|
|
return port
|
|
|
|
|
|
def _main():
|
|
import eventlet
|
|
eventlet.monkey_patch(all=True)
|
|
|
|
from oslo_config import cfg
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
conf = cfg.ConfigOpts()
|
|
conf.register_cli_opts(_options.eventlet_backdoor_opts)
|
|
conf(sys.argv[1:])
|
|
|
|
port_thread = _initialize_if_enabled(conf)
|
|
if not port_thread:
|
|
raise RuntimeError("Did not create backdoor at requested port")
|
|
else:
|
|
_port, thread = port_thread
|
|
thread.wait()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# simple CLI for testing
|
|
_main()
|