emergency commit
[cl-cudd.git] / distr / util / restart.c
blobb81dcb8c8f3b282a5553043cc116d2aa091cee4c
1 #include <stdio.h>
2 #include "util.h"
4 #if (defined(sun) && ! defined(sparc)) || defined(vax)
6 #include <signal.h>
7 #include <sys/types.h>
8 #include <sys/time.h>
10 static char *save_stack_base;
11 static char *stack_lo_addr;
12 static char *stack_hi_addr;
13 static int stack_size;
15 static int restart_global_flag;
16 static char *old_file_name;
17 static char *new_file_name;
19 char *util_save_sp; /* set by util_restart_save_state() */
20 extern char *sbrk();
22 static void
23 grow_stack()
25 int i, space[256];
27 for(i = 0; i < 256; i++) {
28 space[i] = 0;
30 if ((char *) &i > stack_lo_addr) {
31 grow_stack();
36 /* ARGSUSED */
37 static int
38 handle_sigquit(int sig, int code, struct sigcontext *scp)
40 if (util_restart_save_state()) {
41 /* we are restarting ! -- return from signal */
43 } else {
44 /* copy stack to user data space */
45 stack_lo_addr = util_save_sp;
46 stack_size = stack_hi_addr - stack_lo_addr + 1;
47 save_stack_base = sbrk(stack_size);
48 (void) memcpy(save_stack_base, stack_lo_addr, stack_size);
50 /* write a new executable */
51 (void) fprintf(stderr, "Writing executable %s ...\n", new_file_name);
52 (void) util_save_image(old_file_name, new_file_name);
54 /* terminate if signal was a QUIT */
55 if (sig == SIGQUIT) {
56 (void) _exit(1);
62 static void
63 restart_program()
65 (void) fprintf(stderr, "Continuing execution ...\n");
67 /* create the stack */
68 grow_stack();
70 #ifdef vax
71 asm("movl _util_save_sp,sp");
72 #endif
73 #ifdef sun
74 asm("movl _util_save_sp,sp");
75 #endif
77 /* copy the stack back from user space */
78 (void) memcpy(stack_lo_addr, save_stack_base, stack_size);
80 /* remove the sbrk for the stack */
81 if (sbrk(-stack_size) < 0) {
82 perror("sbrk");
85 util_restart_restore_state(); /* jump back into handle_sigquit() */
88 void
89 util_restart(char const *old, char const *neW, int interval)
91 struct itimerval itimer;
93 #ifdef vax
94 #ifdef ultrix
95 stack_hi_addr = (char *) 0x7fffe3ff; /* ultrix */
96 #else
97 stack_hi_addr = (char *) 0x7fffebff; /* bsd 4.3 */
98 #endif
99 #endif
100 #ifdef sun
101 stack_hi_addr = (char *) 0x0effffff; /* Sun OS 3.2, 3.4 */
102 #endif
104 old_file_name = old;
105 new_file_name = neW;
107 (void) signal(SIGQUIT, handle_sigquit);
109 if (interval > 0) {
110 (void) signal(SIGVTALRM, handle_sigquit);
111 itimer.it_interval.tv_sec = interval;
112 itimer.it_interval.tv_usec = 0;
113 itimer.it_value.tv_sec = interval;
114 itimer.it_value.tv_usec = 0;
115 if (setitimer(ITIMER_VIRTUAL, &itimer, (struct itimerval *) 0) < 0) {
116 perror("setitimer");
117 exit(1);
121 if (restart_global_flag) {
122 restart_program();
124 restart_global_flag = 1;
127 #else
129 /* ARGSUSED */
130 void
131 util_restart(char const *old, char const *neW, int interval)
133 (void) fprintf(stderr,
134 "util_restart: not supported on your operating system/hardware\n");
137 #endif