set real thread names (used for identification and diagnostic purposes, e. g. top -H, ps -e -T, pstree, etc)

pull/2573/head
sebres 5 years ago
parent d5144e380e
commit f3dbc9dda1

@ -32,6 +32,13 @@ from threading import Lock
from .server.mytime import MyTime from .server.mytime import MyTime
try:
import ctypes
_libcap = ctypes.CDLL('libcap.so.2')
except:
_libcap = None
PREFER_ENC = locale.getpreferredencoding() PREFER_ENC = locale.getpreferredencoding()
# correct preferred encoding if lang not set in environment: # correct preferred encoding if lang not set in environment:
if PREFER_ENC.startswith('ANSI_'): # pragma: no cover if PREFER_ENC.startswith('ANSI_'): # pragma: no cover
@ -451,6 +458,23 @@ def substituteRecursiveTags(inptags, conditional='',
return tags return tags
if _libcap:
def prctl_set_th_name(name):
"""Helper to set real thread name (used for identification and diagnostic purposes).
"""
try:
if sys.version_info >= (3,): # pragma: 2.x no cover
name = name.encode()
else: # pragma: 3.x no cover
name = bytes(name)
_libcap.prctl(15, name[0:15]) # PR_SET_NAME = 15, name can be up to 15 bytes long (16 bytes with NTS zero)
except:
pass
else: # pragma: no cover
def prctl_set_th_name(name):
pass
class BgService(object): class BgService(object):
"""Background servicing """Background servicing

@ -75,7 +75,7 @@ class Actions(JailThread, Mapping):
""" """
def __init__(self, jail): def __init__(self, jail):
JailThread.__init__(self) JailThread.__init__(self, name="f2b/a."+jail.name)
## The jail which contains this action. ## The jail which contains this action.
self._jail = jail self._jail = jail
self._actions = OrderedDict() self._actions = OrderedDict()

@ -109,6 +109,8 @@ class Filter(JailThread):
self.checkFindTime = True self.checkFindTime = True
## Ticks counter ## Ticks counter
self.ticks = 0 self.ticks = 0
## Thread name:
self.name="f2b/f."+self.jailName
self.dateDetector = DateDetector() self.dateDetector = DateDetector()
logSys.debug("Created %s", self) logSys.debug("Created %s", self)

@ -29,7 +29,7 @@ from threading import Thread
from abc import abstractmethod from abc import abstractmethod
from .utils import Utils from .utils import Utils
from ..helpers import excepthook from ..helpers import excepthook, prctl_set_th_name
class JailThread(Thread): class JailThread(Thread):
@ -76,6 +76,15 @@ class JailThread(Thread):
print(e) print(e)
self.run = run_with_except_hook self.run = run_with_except_hook
if sys.version_info >= (3,): # pragma: 2.x no cover
def _bootstrap(self):
prctl_set_th_name(self.name)
return super(JailThread, self)._bootstrap();
else: # pragma: 3.x no cover
def __bootstrap(self):
prctl_set_th_name(self.name)
return Thread._Thread__bootstrap(self)
@abstractmethod @abstractmethod
def status(self, flavor="basic"): # pragma: no cover - abstract def status(self, flavor="basic"): # pragma: no cover - abstract
"""Abstract - Should provide status information. """Abstract - Should provide status information.
@ -108,4 +117,6 @@ class JailThread(Thread):
if self.active is not None: if self.active is not None:
super(JailThread, self).join() super(JailThread, self).join()
## python 2.x replace binding of private __bootstrap method:
if sys.version_info < (3,): # pragma: 3.x no cover
JailThread._Thread__bootstrap = JailThread._JailThread__bootstrap

@ -38,7 +38,7 @@ from .transmitter import Transmitter
from .asyncserver import AsyncServer, AsyncServerException from .asyncserver import AsyncServer, AsyncServerException
from .. import version from .. import version
from ..helpers import getLogger, _as_bool, extractOptions, str2LogLevel, \ from ..helpers import getLogger, _as_bool, extractOptions, str2LogLevel, \
getVerbosityFormat, excepthook getVerbosityFormat, excepthook, prctl_set_th_name
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = getLogger(__name__) logSys = getLogger(__name__)
@ -80,6 +80,8 @@ class Server:
'Linux': '/dev/log', 'Linux': '/dev/log',
} }
self.__prev_signals = {} self.__prev_signals = {}
# replace real thread name with short process name (for top/ps/pstree or diagnostic):
prctl_set_th_name('f2b/server')
def __sigTERMhandler(self, signum, frame): # pragma: no cover - indirect tested def __sigTERMhandler(self, signum, frame): # pragma: no cover - indirect tested
logSys.debug("Caught signal %d. Exiting", signum) logSys.debug("Caught signal %d. Exiting", signum)

@ -266,7 +266,7 @@ class BasicFilter(unittest.TestCase):
def setUp(self): def setUp(self):
super(BasicFilter, self).setUp() super(BasicFilter, self).setUp()
self.filter = Filter('name') self.filter = Filter(None)
def testGetSetUseDNS(self): def testGetSetUseDNS(self):
# default is warn # default is warn

Loading…
Cancel
Save