Make sure that errno is set properly on exit