Tested by niklas@
Code scavenged from tracker, since it's known to work... removed all special cases, since it's needed only for OpenBSD. Tweaks after some ho@'s comments.