Module: check_mk
Branch: master
Commit: ad719958eafc999170d6d517ffaf2887f4adcce4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ad719958eafc99…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Jun 27 09:40:22 2017 +0200
Added set_cmdline() to set the command of processes (e.g. for ps/top)
Change-Id: I6dc4c048f46c6c06d8b1fb60100b156a22f1a71f
---
lib/daemon.py | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/lib/daemon.py b/lib/daemon.py
index 4127c5f..79ea505 100644
--- a/lib/daemon.py
+++ b/lib/daemon.py
@@ -28,6 +28,8 @@ import os
import sys
from pwd import getpwnam
from grp import getgrnam
+import ctypes
+import ctypes.util
def daemonize(user=0, group=0):
@@ -74,3 +76,25 @@ def daemonize(user=0, group=0):
os.dup2(so, 2)
os.close(si)
os.close(so)
+
+
+def set_cmdline(cmdline):
+ """
+ Change the process name and process command line on of the running process
+ This works at least with Python 2.x on Linux
+ """
+ libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
+
+ argv = ctypes.POINTER(ctypes.c_char_p)()
+ argc = ctypes.c_int()
+ ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv))
+ cmdlen = sum([len(argv[i]) for i in range(argc.value)]) + argc.value
+ new_cmdline = ctypes.c_char_p(cmdline.ljust(cmdlen, '\0'))
+
+ # replace the command line, which is available via /proc/<pid>/cmdline.
+ # This is .e.g used by ps
+ libc.memcpy(argv.contents, new_cmdline, cmdlen)
+
+ # replace the prctl name, which is available via /proc/<pid>/status.
+ # This is for example used by top and killall
+ libc.prctl(15, new_cmdline, 0, 0, 0)