use at_exit, fix file modes