Can't ptrace child started with subprocess.Popen from other child
cmlsharp opened this issue · 2 comments
I apologize if this is an ignorant question. I have a parent process which spawns two children and I need one of the children (created via multiprocessing
) to be able to ptrace the other (started via subprocess.Popen
) but I am recieving the permission error: ptrace.error.PtraceError: ptrace(cmd=16, pid=15151, 0, 0) error #1: Operation not permitted
Here is sample code that demonstrates what I am trying to do and the error I am encountering:
import multiprocessing
import subprocess
import time
import ptrace.debugger
def ptrace_test(pid):
debugger = ptrace.debugger.PtraceDebugger()
process = debugger.addProcess(pid, False)
# Other stuff
child = subprocess.Popen("/usr/bin/ls")
proc = multiprocessing.Process(target=ptrace_test, args=(child.pid,))
proc.start()
proc.join()
FYI I'm not working anymore on this project and ptrace remains a big mystery for me :-) Good luck to find the correct way to use ptrace :-D
There are several reasons this can happen, see here.
If due to the YAMA feature (probably if on recent Ubuntu), then you can use prctl.set_ptracer
in the tracee with the tracer pid and signal.pause()
until the tracer attaches. You may be able to use the preexec_fn
argument to Popen for this but there are a lot of warnings about it in the documentation - it may be better to start a multiprocessing.Process that creates the subprocess after the tracer attaches.