SHARE
TWEET

restart main via longjmp atexit

a guest Nov 24th, 2014 317 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <setjmp.h>  // setjmp/longjmp
  2. #include <unistd.h>  // sleep
  3. #include <stdio.h>   // printf
  4. #include <stdlib.h>  // exit, atexit
  5.  
  6. jmp_buf panic_restart;
  7. int panic_restart_count=0;
  8.  
  9. void
  10. do_the_panic_restart()
  11. {
  12.         if (panic_restart_count++ < 3)
  13.                 longjmp(panic_restart, 1);
  14. }
  15.  
  16. int
  17. main(int argc, char **argv)
  18. {
  19.         if(setjmp(panic_restart)) {
  20.                 fprintf(stderr,"PANIC! main() has been restarted due to a unscheduled exit!\n");
  21.         }
  22.         atexit(&do_the_panic_restart);
  23.  
  24.         printf("This is main. I will sleep a while, then try to exit.\n");
  25.         sleep(1);
  26.         exit(1);
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top