@@ -31,19 +31,24 @@ def detach(pid: int) -> None:
3131def attach (args : argparse .Namespace ) -> None :
3232 from hypno import inject_py
3333
34- script = dedent (
35- f"""
36- from echion.bootstrap.attach import attach
37- attach({ args .__dict__ !r} )
38- """
39- ).strip ()
40-
4134 pid = args .pid or args .where
4235
4336 try :
37+ pipe_name = None
4438 if args .where :
4539 pipe_name = Path (tempfile .gettempdir ()) / f"echion-{ pid } "
4640 os .mkfifo (pipe_name )
41+ # This named pipe is likely created by the superuser, so we need to
42+ # make it writable by everyone to allow the target process to write
43+ # to it.
44+ os .chmod (pipe_name , 0o666 )
45+
46+ script = dedent (
47+ f"""
48+ from echion.bootstrap.attach import attach
49+ attach({ args .__dict__ !r} , { repr (str (pipe_name )) if pipe_name is not None else str (None )} )
50+ """
51+ ).strip ()
4752
4853 inject_py (pid , script )
4954
@@ -53,6 +58,7 @@ def attach(args: argparse.Namespace) -> None:
5358 from time import monotonic as time
5459
5560 end = time () + args .exposure
61+
5662 while not args .where :
5763 try :
5864 os .kill (pid , 0 )
@@ -61,11 +67,12 @@ def attach(args: argparse.Namespace) -> None:
6167 if end is not None and time () > end :
6268 break
6369 os .sched_yield ()
70+
6471 except (KeyboardInterrupt , ProcessLookupError ):
6572 pass
6673
6774 # Read the output
68- if args .where and pipe_name .exists ():
75+ if args .where and pipe_name is not None and pipe_name .exists ():
6976 with pipe_name .open ("r" ) as f :
7077 while True :
7178 line = f .readline ()
@@ -76,7 +83,7 @@ def attach(args: argparse.Namespace) -> None:
7683 detach (pid )
7784
7885 finally :
79- if args .where and pipe_name .exists ():
86+ if args .where and pipe_name is not None and pipe_name .exists ():
8087 pipe_name .unlink ()
8188
8289
0 commit comments