BUGFIX: Be a lot more clever about what the path is

Previously if the pid number had wrapped then the wrong path was
selected (oops).  So actually work our way up and down the process
tree as needed to work out what's the right thing to do