source: icGREP/icgrep-devel/cudd-2.5.1/util/restart.c @ 5820

Last change on this file since 5820 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 2.8 KB
Line 
1#include <stdio.h>
2#include "util.h"
3
4#if (defined(sun) && ! defined(sparc)) || defined(vax)
5
6#include <signal.h>
7#include <sys/types.h>
8#include <sys/time.h>
9
10static char *save_stack_base;
11static char *stack_lo_addr;
12static char *stack_hi_addr;
13static int stack_size;
14
15static int restart_global_flag;
16static char *old_file_name;
17static char *new_file_name;
18
19char *util_save_sp;             /* set by util_restart_save_state() */
20extern char *sbrk();
21
22static void
23grow_stack() 
24{
25    int i, space[256];
26
27    for(i = 0; i < 256; i++) {
28        space[i] = 0;
29    }
30    if ((char *) &i > stack_lo_addr) {
31        grow_stack();
32    }
33}
34
35
36/* ARGSUSED */
37static int
38handle_sigquit(int sig, int code, struct sigcontext *scp)
39{
40    if (util_restart_save_state()) {
41        /* we are restarting ! -- return from signal */
42
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);
49
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);
53
54        /* terminate if signal was a QUIT */
55        if (sig == SIGQUIT) {
56            (void) _exit(1);
57        }
58    }
59}
60
61
62static void
63restart_program()
64{
65    (void) fprintf(stderr, "Continuing execution ...\n");
66
67    /* create the stack */
68    grow_stack();
69
70#ifdef vax
71    asm("movl _util_save_sp,sp");
72#endif
73#ifdef sun
74    asm("movl _util_save_sp,sp");
75#endif
76
77    /* copy the stack back from user space */
78    (void) memcpy(stack_lo_addr, save_stack_base, stack_size);
79
80    /* remove the sbrk for the stack */
81    if (sbrk(-stack_size) < 0) {
82        perror("sbrk");
83    }
84
85    util_restart_restore_state();       /* jump back into handle_sigquit() */
86}
87
88void
89util_restart(char const *old, char const *neW, int interval)
90{
91    struct itimerval itimer;
92
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
103
104    old_file_name = old;
105    new_file_name = neW;
106
107    (void) signal(SIGQUIT, handle_sigquit);
108
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);
118        }
119    }
120
121    if (restart_global_flag) {
122        restart_program();
123    }
124    restart_global_flag = 1;
125}
126
127#else
128
129/* ARGSUSED */
130void
131util_restart(char const *old, char const *neW, int interval)
132{
133    (void) fprintf(stderr, 
134        "util_restart: not supported on your operating system/hardware\n");
135}
136
137#endif
Note: See TracBrowser for help on using the repository browser.