From af85c766fc22f73fb604510ae5b7c471b923990d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 3 Apr 2015 02:25:33 +0200 Subject: set title on lightweight processes, properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/__main__.py | 27 --------------------------- src/util.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/__main__.py b/src/__main__.py index cfda11d..f809a5d 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -478,33 +478,6 @@ class Bar: ## Set process title -def setproctitle(title): - ''' - Set process title - - @param title:str The title of the process - ''' - import ctypes - try: - # Remove path, keep only the file, - # otherwise we get really bad effects, namely - # the name title is truncates by the number - # of slashes in the title. At least that is - # the observed behaviour when using procps-ng. - title = title.split('/')[-1] - # Create string buffer with title - title = title.encode(sys.getdefaultencoding(), 'replace') - title = ctypes.create_string_buffer(title) - if 'linux' in sys.platform: - # Set process title on Linux - libc = ctypes.cdll.LoadLibrary('libc.so.6') - libc.prctl(15, ctypes.byref(title), 0, 0, 0) - elif 'bsd' in sys.platform: - # Set process title on at least FreeBSD - libc = ctypes.cdll.LoadLibrary('libc.so.7') - libc.setproctitle(ctypes.create_string_buffer(b'-%s'), title) - except: - pass setproctitle(sys.argv[0]) diff --git a/src/util.py b/src/util.py index a966bfd..10e34db 100644 --- a/src/util.py +++ b/src/util.py @@ -22,6 +22,35 @@ import threading import subprocess +def setproctitle(title): + ''' + Set process title + + @param title:str The title of the process + ''' + import ctypes + try: + # Remove path, keep only the file, + # otherwise we get really bad effects, namely + # the name title is truncates by the number + # of slashes in the title. At least that is + # the observed behaviour when using procps-ng. + title = title.split('/')[-1] + # Create string buffer with title + title = title.encode(sys.getdefaultencoding(), 'replace') + title = ctypes.create_string_buffer(title) + if 'linux' in sys.platform: + # Set process title on Linux + libc = ctypes.cdll.LoadLibrary('libc.so.6') + libc.prctl(15, ctypes.byref(title), 0, 0, 0) + elif 'bsd' in sys.platform: + # Set process title on at least FreeBSD + libc = ctypes.cdll.LoadLibrary('libc.so.7') + libc.setproctitle(ctypes.create_string_buffer(b'-%s'), title) + except: + pass + + def async(target, name = None, group = None): ''' Start a function asynchronously @@ -30,7 +59,11 @@ def async(target, name = None, group = None): @param name:str? The name of the thread @return :Thread A running deamon-thread running `target`, with the name `name` ''' - t = threading.Thread(target = target, name = name) + def target_wrapper(): + if 'linux' in sys.platform: ## XXX I only know that will happen on Linux + setproctitle(name) + target() + t = threading.Thread(target = target_wrapper if name is not None else target, name = name) t.setDaemon(True) t.start() return t -- cgit v1.2.3-70-g09d2