Skip to content

Commit

Permalink
fix: ensure named pipes are cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 committed Oct 14, 2023
1 parent 95f2fb1 commit b45da9d
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions echion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,44 @@ def attach(args: argparse.Namespace) -> None:

pid = args.pid or args.where

if args.where:
pipe_name = Path(tempfile.gettempdir()) / f"echion-{pid}"
os.mkfifo(pipe_name)
try:
if args.where:
pipe_name = Path(tempfile.gettempdir()) / f"echion-{pid}"
os.mkfifo(pipe_name)

inject_py(pid, script)
inject_py(pid, script)

try:
end = None
if args.exposure:
from time import monotonic as time

end = time() + args.exposure
while not args.where:
try:
os.kill(pid, 0)
except ProcessLookupError:
break
if end is not None and time() > end:
break
os.sched_yield()
except (KeyboardInterrupt, ProcessLookupError):
pass

# Read the output
if args.where:
with pipe_name.open("r") as f:
while True:
line = f.readline()
print(line, end="")
if not line:
try:
end = None
if args.exposure:
from time import monotonic as time

end = time() + args.exposure
while not args.where:
try:
os.kill(pid, 0)
except ProcessLookupError:
break
if end is not None and time() > end:
break
pipe_name.unlink()
os.sched_yield()
except (KeyboardInterrupt, ProcessLookupError):
pass

detach(pid)
# Read the output
if args.where and pipe_name.exists():
with pipe_name.open("r") as f:
while True:
line = f.readline()
print(line, end="")
if not line:
break

detach(pid)

finally:
if args.where and pipe_name.exists():
pipe_name.unlink()


def main() -> None:
Expand Down Expand Up @@ -193,3 +197,7 @@ def main() -> None:
)
parser.print_usage()
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit b45da9d

Please sign in to comment.