summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-10-13 22:38:18 +0200
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-10-13 22:41:20 +0200
commit87346e06adbc1bd5c727df3947b8dd7149f5b7d5 (patch)
tree9ecb419e5f858bdc63dc6849e0cfe376fcd16e5d
Add first version that is allowing to enable keepalive
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rw-r--r--solter.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/solter.py b/solter.py
new file mode 100644
index 0000000..6de8000
--- /dev/null
+++ b/solter.py
@@ -0,0 +1,52 @@
+import argparse
+import subprocess
+import logging
+import pprint
+
+
+SO_SOCKET = 1
+
+SO_KEEPALIVE = 9
+
+
+def get_commands_enable_keepalive(sockfd: int):
+ return (
+ "set $rsp -= sizeof(int)",
+ "set {int}$rsp = 1",
+ f"call (int)setsockopt({sockfd}, {SO_SOCKET}, {SO_KEEPALIVE}, $rsp, sizeof(int))",
+ "set $rsp += sizeof(int)",
+ )
+
+
+def main(args):
+ numeric_level = getattr(logging, args["loglevel"].upper(), None)
+ if not isinstance(numeric_level, int):
+ raise ValueError(f"Invalid log level: {args['loglevel']}")
+ logging.basicConfig(level=numeric_level)
+
+
+ cmd = ["gdb", "--pid", str(args["pid"]), "--batch-silent"]
+
+ gdb_cmds = list()
+ gdb_cmds.extend(get_commands_enable_keepalive(args["sockfd"]))
+
+ logging.debug("gdb commands: %s", pprint.pformat(gdb_cmds))
+
+ cmd.extend(map(lambda gdb_cmd: f"--eval-command={gdb_cmd}", gdb_cmds))
+
+ logging.debug("Executing: %s", pprint.pformat(cmd))
+
+ subprocess.run(cmd)
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+
+ parser.add_argument("--pid", type=int, required=True,
+ help="PID of the running process to alter")
+ parser.add_argument("--sockfd", type=int, required=True,
+ help="Socket file-descriptor number to alter")
+ parser.add_argument("--loglevel", default="info",
+ help="Change log level")
+
+ main(vars(parser.parse_args()))