I think you need to add here
   atexit(regain_privs_atexit);
(after, since the order for atexit calls is the reverse)