commit 64e518175d66ee19a0c493dc323ba653fc9bb4de Author: Damian Johnson atagar@torproject.org Date: Sat Jan 12 23:47:59 2013 -0800
Using stem's process renaming
Using the process renaming functionality I recently moved to stem. --- src/starter.py | 4 +- src/util/__init__.py | 2 +- src/util/procName.py | 117 -------------------------------------------------- 3 files changed, 3 insertions(+), 120 deletions(-)
diff --git a/src/starter.py b/src/starter.py index 498ba99..303c342 100644 --- a/src/starter.py +++ b/src/starter.py @@ -29,6 +29,7 @@ from stem.control import Controller import stem.connection import stem.util.conf import stem.util.log +import stem.util.system
LOG_DUMP_PATH = os.path.expanduser("~/.arm/log") DEFAULT_CONFIG = os.path.expanduser("~/.arm/armrc") @@ -465,8 +466,7 @@ if __name__ == '__main__': # "arm <input args>"
try: - from util import procName - procName.renameProcess("arm\0%s" % "\0".join(sys.argv[1:])) + stem.util.system.set_process_name("arm\0%s" % "\0".join(sys.argv[1:])) except: pass
# If using our LANG variable for rendering multi-byte characters lets us diff --git a/src/util/__init__.py b/src/util/__init__.py index 16b28ee..3e21520 100644 --- a/src/util/__init__.py +++ b/src/util/__init__.py @@ -4,5 +4,5 @@ application's status, making cross platform system calls, parsing tor data, and safely working with curses (hiding some of the gory details). """
-__all__ = ["connections", "hostnames", "panel", "procName", "sysTools", "textInput", "torConfig", "torTools", "uiTools"] +__all__ = ["connections", "hostnames", "panel", "sysTools", "textInput", "torConfig", "torTools", "uiTools"]
diff --git a/src/util/procName.py b/src/util/procName.py deleted file mode 100644 index b33ff7b..0000000 --- a/src/util/procName.py +++ /dev/null @@ -1,117 +0,0 @@ -""" -Module to allow for arbitrary renaming of our python process. This is mostly -based on: -http://www.rhinocerus.net/forum/lang-python/569677-setting-program-name-like... -and an adaptation by Jake: https://github.com/ioerror/chameleon - -A cleaner implementation is available at: -https://github.com/cream/libs/blob/b38970e2a6f6d2620724c828808235be0445b799/... -but I'm not quite clear on their implementation, and it only does targeted -argument replacement (ie, replace argv[0], argv[1], etc but with a string -the same size). -""" - -import ctypes -import ctypes.util -import platform - -# flag for setting the process name, found in '/usr/include/linux/prctl.h' -PR_SET_NAME = 15 - -argc_t = ctypes.POINTER(ctypes.c_char_p) - -Py_GetArgcArgv = ctypes.pythonapi.Py_GetArgcArgv -Py_GetArgcArgv.restype = None -Py_GetArgcArgv.argtypes = [ctypes.POINTER(ctypes.c_int), - ctypes.POINTER(argc_t)] - -# tracks the last name we've changed the process to -currentProcessName = None -maxNameLength = -1 - -def renameProcess(processName): - """ - Renames our current process from "python <args>" to a custom name. - - Arguments: - processName - new name for our process - """ - - _setArgv(processName) - if platform.system() == "Linux": - _setPrctlName(processName) - elif platform.system() in ("Darwin", "FreeBSD", "OpenBSD"): - _setProcTitle(processName) - -def _setArgv(processName): - """ - Overwrites our argv in a similar fashion to how it's done in C with: - strcpy(argv[0], "new_name"); - """ - - global currentProcessName, maxNameLength - - argv = ctypes.c_int(0) - argc = argc_t() - Py_GetArgcArgv(argv, ctypes.pointer(argc)) - - # The original author did the memset for 256, while Jake did it for the - # processName length (capped at 1608). I'm not sure of the reasons for - # either of these limits, but setting it to anything higher than than the - # length of the null terminated process name should be pointless, so opting - # for Jake's implementation on this. - - if currentProcessName == None: - # Getting argv via... - # currentProcessName = " ".join(["python"] + sys.argv) - # - # doesn't do the trick since this will miss interpretor arguments like... - # python -W ignore::DeprecationWarning myScript.py - # - # Hence we're fetching this via our ctypes argv. Alternatively we could - # use ps, though this is less desirable: - # "ps -p %i -o args" % os.getpid() - - args = [] - for i in range(100): - if argc[i] == None: break - args.append(str(argc[i])) - - currentProcessName = " ".join(args) - maxNameLength = len(currentProcessName) - - if len(processName) > maxNameLength: - msg = "can't rename process to something longer than our initial name since this would overwrite memory used for the env" - raise IOError(msg) - - # space we need to clear - zeroSize = max(len(currentProcessName), len(processName)) - - ctypes.memset(argc.contents, 0, zeroSize + 1) # null terminate the string's end - ctypes.memmove(argc.contents, processName, len(processName)) - currentProcessName = processName - -def _setPrctlName(processName): - """ - Sets the prctl name, which is used by top and killall. This appears to be - Linux specific and has the max of 15 characters. Source: - http://stackoverflow.com/questions/564695/is-there-a-way-to-change-effective... - """ - - libc = ctypes.CDLL(ctypes.util.find_library("c")) - nameBuffer = ctypes.create_string_buffer(len(processName)+1) - nameBuffer.value = processName - libc.prctl(PR_SET_NAME, ctypes.byref(nameBuffer), 0, 0, 0) - -def _setProcTitle(processName): - """ - BSD specific calls (should be compataible with both FreeBSD and OpenBSD: - http://fxr.watson.org/fxr/source/gen/setproctitle.c?v=FREEBSD-LIBC - http://www.rootr.net/man/man/setproctitle/3 - """ - - libc = ctypes.CDLL(ctypes.util.find_library("c")) - nameBuffer = ctypes.create_string_buffer(len(processName)+1) - nameBuffer.value = processName - libc.setproctitle(ctypes.byref(nameBuffer)) -