Ported by me from OpenBSD, improvements from Kevin Lahey, successfully tested by Kevin Lahey and Stephen Borrill.