A bit of refactoring necessary before we can move the remaining Linux specific code to the frontend. Most of this taken from #3535 but attempting to be NFC as much as possible.