Page MenuHomeFreeBSD

proc_realparent
ClosedPublic

Authored by kib on Jul 15 2014, 7:26 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Feb 7, 4:41 PM
Unknown Object (File)
Nov 3 2025, 3:01 AM
Unknown Object (File)
Nov 2 2025, 10:55 PM
Unknown Object (File)
Nov 2 2025, 6:34 PM
Unknown Object (File)
Oct 4 2025, 9:10 PM
Unknown Object (File)
Sep 17 2025, 11:25 AM
Unknown Object (File)
Jun 27 2025, 12:41 PM
Unknown Object (File)
Jun 27 2025, 3:30 AM
Subscribers

Details

Reviewers
kib
jhb
Summary

Patch below corrects the long standing and well known problem with the ptrace(2) making the debuggee an orphan. Since the tracking for orphans was added to properly handle the delivery of SIGCHLD to real parent, it can also be reused for other things.

Among them are the problem with inferior(9) looping due to the process tree becoming a graph instead of tree if the parent is traced by child. Another kind of issues is due to the use of p_oppid to restore the original parent/child relationship, because real parent could already exited and its pid reused (noted by mjg).

Patch adds the function proc_realparent(9), which calculates the parent for given process. It uses the trick of finding the head element of the p_orphan list and than stepping back to its container to find the parent process. If the parent has already exited, the init(8) is returned.

I moved the P_ORPHAN and the new helper flag from the p_flag* to new p_treeflag field of struct proc, which is protected by proctree lock instead of proc lock. This makes the list traversals much simpler, because you do not need to relock proc locks for all process which are found. The orphans relationship is managed under the proctree_lock already.

The remaining uses of p_oppid in ptrace(PT_DETACH) and process reapping are replaced by proc_realparent(9).

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

kib retitled this revision from to proc_realparent.
kib updated this object.
kib edited the test plan for this revision. (Show Details)

One more note. I was rightfully asked why not just save a pointer to real parent in the struct proc, and be done with it. The pointer would be protected by the same proctree_lock and have the same lifetime as the orphan list, so its maintanence is not hard.

I considered this approach, but decided, since orphans still should be there, to not increase the amount of interconnections in struct proc. Note that typical orphan list consists of only one item, so the real_parent calculation even in the reparented case typically consists in the flag check, pointer dereference and addition.

I think this looks fine. At one point I had started on changing ptrace() to not alter p_pptr at all, but instead using separate p_debugger and p_debugee linkages in struct proc to link debuggers to debugees and vice versa. In this case, p_pptr is always the real parent. What I hadn't worked on was fixing wait() to work properly in this setup. I do still like that approach long term.

kib added a reviewer: kib.
This revision is now accepted and ready to land.Apr 24 2015, 4:35 PM