This is essentially cherry-picked from sbase with slight modifications as we can now assume that it will be running on Linux.