From 785ed9fdda916ee78a51fe7c71f0b5177afa1c7c Mon Sep 17 00:00:00 2001 From: k4lizen <124312252+k4lizen@users.noreply.github.com> Date: Fri, 27 Sep 2024 09:23:06 +0200 Subject: [PATCH] Properly close spawned kitty window (#2471) * properly close spawned kitty window * python2 support * changelog --- CHANGELOG.md | 2 ++ pwnlib/util/misc.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f4f7a0d..97f016d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,10 +72,12 @@ The table below shows which release corresponds to each branch, and what date th ## 4.15.0 (`dev`) +- [#2471][2471] Properly close spawned kitty window - [#2358][2358] Cache output of `asm()` - [#2457][2457] Catch exception of non-ELF files in checksec. - [#2444][2444] Add `ELF.close()` to release resources +[2471]: https://github.com/Gallopsled/pwntools/pull/2471 [2358]: https://github.com/Gallopsled/pwntools/pull/2358 [2457]: https://github.com/Gallopsled/pwntools/pull/2457 [2444]: https://github.com/Gallopsled/pwntools/pull/2444 diff --git a/pwnlib/util/misc.py b/pwnlib/util/misc.py index d1622154e..90ccb1576 100644 --- a/pwnlib/util/misc.py +++ b/pwnlib/util/misc.py @@ -443,11 +443,13 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr log.debug("Launching a new terminal: %r" % argv) stdin = stdout = stderr = open(os.devnull, 'r+b') - if terminal == 'tmux': + if terminal == 'tmux' or terminal == 'kitty': stdout = subprocess.PIPE p = subprocess.Popen(argv, stdin=stdin, stdout=stdout, stderr=stderr, preexec_fn=preexec_fn) + kittyid = None + if terminal == 'tmux': out, _ = p.communicate() try: @@ -460,6 +462,16 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr with subprocess.Popen((qdbus, konsole_dbus_service, '/Sessions/{}'.format(last_konsole_session), 'org.kde.konsole.Session.processId'), stdout=subprocess.PIPE) as proc: pid = int(proc.communicate()[0].decode()) + elif terminal == 'kitty': + pid = p.pid + + out, _ = p.communicate() + try: + kittyid = int(out) + except ValueError: + kittyid = None + if kittyid is None: + log.error("Could not parse kitty window ID from output (%r)", out) else: pid = p.pid @@ -468,6 +480,8 @@ def kill(): try: if terminal == 'qdbus': os.kill(pid, signal.SIGHUP) + elif terminal == 'kitty': + subprocess.Popen(["kitten", "@", "close-window", "--match", "id:{}".format(kittyid)]) else: os.kill(pid, signal.SIGTERM) except OSError: