source: git/Singular/cntrlc.cc @ 655e1f

spielwiese
Last change on this file since 655e1f was 655e1f, checked in by Hans Schoenemann <hannes@…>, 13 years ago
singular_in_batchmode git-svn-id: file:///usr/local/Singular/svn/trunk@13935 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT - interupt handling
7*/
8
9/* includes */
10#ifdef DecAlpha_OSF1
11#define _XOPEN_SOURCE_EXTENDED
12#endif /* MP3-Y2 0.022UF */
13#include <stdio.h>
14#include <stddef.h>
15#include <stdlib.h>
16#include <strings.h>
17#include <signal.h>
18#include <sys/types.h>
19#include <sys/wait.h>
20#include <kernel/mod2.h>
21#include <omalloc/omalloc.h>
22#include <Singular/tok.h>
23#include <Singular/ipshell.h>
24#include <kernel/febase.h>
25#include <Singular/cntrlc.h>
26#include <kernel/polys.h>
27#include <Singular/feOpt.h>
28#include <Singular/version.h>
29#include <Singular/silink.h>
30
31/* undef, if you don't want GDB to come up on error */
32
33#if !defined(__alpha)
34#define CALL_GDB
35#endif
36
37#if defined(__OPTIMIZE__) && defined(CALL_GDB)
38#undef CALL_GDB
39#endif
40
41#if defined(unix) && !defined(hpux)
42 #include <unistd.h>
43 #include <sys/types.h>
44
45 #ifdef TIME_WITH_SYS_TIME
46   #include <time.h>
47   #ifdef HAVE_SYS_TIME_H
48     #include <sys/time.h>
49   #endif
50 #else
51   #ifdef HAVE_SYS_TIME_H
52     #include <sys/time.h>
53   #else
54     #include <time.h>
55   #endif
56 #endif
57 #ifdef HAVE_SYS_TIMES_H
58   #include <sys/times.h>
59 #endif
60
61 #define INTERACTIVE 0
62 #define STACK_TRACE 1
63
64 #ifdef CALL_GDB
65   static void debug (int);
66   static void debug_stop (char *const*args);
67 #endif
68 #ifndef __OPTIMIZE__
69   static void stack_trace_sigchld (int);
70   static void stack_trace (char *const*args);
71 #endif
72#endif
73
74si_link pipeLastLink=NULL;
75BOOLEAN singular_in_batchmode=FALSE;
76
77void sig_pipe_hdl(int sig)
78{
79 if (pipeLastLink!=NULL)
80 {
81   slClose(pipeLastLink);
82   pipeLastLink=NULL;
83   WerrorS("pipe failed");
84 }
85}
86
87/*---------------------------------------------------------------------*
88 * File scope Variables (Variables share by several functions in
89 *                       the same file )
90 *
91 *---------------------------------------------------------------------*/
92/* data */
93jmp_buf si_start_jmpbuf;
94int siRandomStart;
95short si_restart=0;
96BOOLEAN siCntrlc = FALSE;
97
98typedef void (*si_hdl_typ)(int);
99
100
101/*0 implementation*/
102/*---------------------------------------------------------------------*
103 * Functions declarations
104 *
105 *---------------------------------------------------------------------*/
106void sigint_handler(int sig);
107
108si_hdl_typ si_set_signal ( int sig, si_hdl_typ signal_handler);
109
110/*---------------------------------------------------------------------*/
111/**
112 * @brief meta function for binding a signal to an handler
113
114 @param[in] sig             Signal number
115 @param[in] signal_handler  Pointer to signal handler
116
117 @return value of signal()
118**/
119/*---------------------------------------------------------------------*/
120si_hdl_typ si_set_signal ( int sig, si_hdl_typ signal_handler)
121{
122  si_hdl_typ retval=signal (sig, (si_hdl_typ)signal_handler);
123  if (retval == SIG_ERR)
124  {
125     fprintf(stderr, "Unable to init signal %d ... exiting...\n", sig);
126  }
127#ifdef HAVE_SIGINTERRUPT
128  siginterrupt(sig, 1);
129#endif
130  return retval;
131}                               /* si_set_signal */
132
133
134/*---------------------------------------------------------------------*/
135#if defined(ix86_Linux)
136  #if !defined(HAVE_SIGCONTEXT) && !defined(HAVE_ASM_SIGCONTEXT_H)
137// we need the following structure sigcontext_struct.
138// if configure finds asm/singcontext.h we assume
139// that this file contains the structure and is included
140// via signal.h
141struct sigcontext_struct {
142        unsigned short gs, __gsh;
143        unsigned short fs, __fsh;
144        unsigned short es, __esh;
145        unsigned short ds, __dsh;
146        unsigned long edi;
147        unsigned long esi;
148        unsigned long ebp;
149        unsigned long esp;
150        unsigned long ebx;
151        unsigned long edx;
152        unsigned long ecx;
153        unsigned long eax;
154        unsigned long trapno;
155        unsigned long err;
156        unsigned long eip;
157        unsigned short cs, __csh;
158        unsigned long eflags;
159        unsigned long esp_at_signal;
160        unsigned short ss, __ssh;
161        unsigned long i387;
162        unsigned long oldmask;
163        unsigned long cr2;
164};
165#endif
166#define HAVE_SIGSTRUCT
167typedef struct sigcontext_struct sigcontext;
168#endif
169
170#if defined(x86_64_Linux)
171#define HAVE_SIGSTRUCT
172#endif
173
174
175#if defined(HAVE_SIGSTRUCT)
176/*2---------------------------------------------------------------------*/
177/**
178 * @brief signal handler for run time errors, linux/i386, x86_64 version
179
180 @param[in] sig
181 @param[in] s
182**/
183/*---------------------------------------------------------------------*/
184void sigsegv_handler(int sig, sigcontext s)
185{
186  fprintf(stderr,"Singular : signal %d (v: %d/%s):\n",sig,SINGULAR_VERSION,feVersionId);
187  if (sig!=SIGINT)
188  {
189    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
190    fprintf(stderr,"Segment fault/Bus error occurred at %lx because of %lx (r:%d)\n"
191                   "please inform the authors\n",
192                   #ifdef __i386__
193                   (long)s.eip,
194                   #else /* x86_64*/
195                   (long)s.rip,
196                   #endif
197                   (long)s.cr2,siRandomStart);
198  }
199#ifdef __OPTIMIZE__
200  if(si_restart<3)
201  {
202    si_restart++;
203    fprintf(stderr,"trying to restart...\n");
204    init_signals();
205    longjmp(si_start_jmpbuf,1);
206  }
207#endif /* __OPTIMIZE__ */
208#ifdef CALL_GDB
209  if (sig!=SIGINT) debug(INTERACTIVE);
210#endif /* CALL_GDB */
211  exit(0);
212}
213
214/*---------------------------------------------------------------------*/
215/**
216 * @brief additional default signal handler
217
218  // some newer Linux version cannot have SIG_IGN for SIGCHLD,
219  // so use this nice routine here:
220  //  SuSe 9.x reports -1 always
221  //  Redhat 9.x/FC x reports sometimes -1
222  // see also: hpux_system
223  // also needed by getrusage (timer etc.)
224
225 @param[in] sig
226**/
227/*---------------------------------------------------------------------*/
228void sig_ign_hdl(int sig)
229{
230 waitpid(-1,NULL,WNOHANG); 
231}
232
233/*2
234* init signal handlers, linux/i386 version
235*/
236void init_signals()
237{
238/*4 signal handler: linux*/
239  if (SIG_ERR==si_set_signal(SIGSEGV,(si_hdl_typ)sigsegv_handler))
240  {
241    PrintS("cannot set signal handler for SEGV\n");
242  }
243  if (SIG_ERR==si_set_signal(SIGFPE, (si_hdl_typ)sigsegv_handler))
244  {
245    PrintS("cannot set signal handler for FPE\n");
246  }
247  if (SIG_ERR==si_set_signal(SIGILL, (si_hdl_typ)sigsegv_handler))
248  {
249    PrintS("cannot set signal handler for ILL\n");
250  }
251  if (SIG_ERR==si_set_signal(SIGIOT, (si_hdl_typ)sigsegv_handler))
252  {
253    PrintS("cannot set signal handler for IOT\n");
254  }
255  if (SIG_ERR==si_set_signal(SIGINT ,(si_hdl_typ)sigint_handler))
256  {
257    PrintS("cannot set signal handler for INT\n");
258  }
259  si_set_signal(SIGCHLD, (si_hdl_typ)sig_ign_hdl);
260  si_set_signal(SIGPIPE, (si_hdl_typ)sig_pipe_hdl);
261}
262
263/*---------------------------------------------------------------------*/
264#elif defined(SunOS) /*SPARC_SUNOS*/
265/*2
266* signal handler for run time errors, sparc sunos 4 version
267*/
268void sigsegv_handler(int sig, int code, struct sigcontext *scp, char *addr)
269{
270  fprintf(stderr,"Singular : signal %d, code %d (v: %d/%s):\n",
271    sig,code,SINGULAR_VERSION,feVersionId);
272  if ((sig!=SIGINT)&&(sig!=SIGABRT))
273  {
274    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
275    fprintf(stderr,"Segment fault/Bus error occurred at %x (r:%d)\n"
276                   "please inform the authors\n",
277                   (int)addr,siRandomStart);
278  }
279#ifdef __OPTIMIZE__
280  if(si_restart<3)
281  {
282    si_restart++;
283    fprintf(stderr,"trying to restart...\n");
284    init_signals();
285    longjmp(si_start_jmpbuf,1);
286  }
287#endif /* __OPTIMIZE__ */
288#ifdef CALL_GDB
289  if (sig!=SIGINT) debug(STACK_TRACE);
290#endif /* CALL_GDB */
291  exit(0);
292}
293
294/*2
295* init signal handlers, sparc sunos 4 version
296*/
297void init_signals()
298{
299/*4 signal handler:*/
300  si_set_signal(SIGSEGV,sigsegv_handler);
301  si_set_signal(SIGBUS, sigsegv_handler);
302  si_set_signal(SIGFPE, sigsegv_handler);
303  si_set_signal(SIGILL, sigsegv_handler);
304  si_set_signal(SIGIOT, sigsegv_handler);
305  si_set_signal(SIGINT ,sigint_handler);
306  si_set_signal(SIGCHLD, (void (*)(int))SIG_IGN);
307  si_set_signal(SIGPIPE, (si_hdl_typ)sig_pipe_hdl);
308}
309#else
310
311/*---------------------------------------------------------------------*/
312/*2
313* signal handler for run time errors, general version
314*/
315void sigsegv_handler(int sig)
316{
317  fprintf(stderr,"Singular : signal %d (v: %d/%s):\n",
318    sig,SINGULAR_VERSION,feVersionId);
319  if (sig!=SIGINT)
320  {
321    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
322    fprintf(stderr,"Segment fault/Bus error occurred (r:%d)\n"
323                   "please inform the authors\n",
324                   siRandomStart);
325  }
326  #ifdef __OPTIMIZE__
327  if(si_restart<3)
328  {
329    si_restart++;
330    fprintf(stderr,"trying to restart...\n");
331    init_signals();
332    longjmp(si_start_jmpbuf,1);
333  }
334  #endif /* __OPTIMIZE__ */
335  #if defined(unix) && !defined(hpux)
336  /* debug(..) does not work under HPUX (because ptrace does not work..) */
337  #ifdef CALL_GDB
338  if (sig!=SIGINT) debug(STACK_TRACE);
339  #endif /* CALL_GDB */
340  #endif /* unix */
341  exit(0);
342}
343
344/*2
345* init signal handlers, general version
346*/
347void init_signals()
348{
349/*4 signal handler:*/
350  si_set_signal(SIGSEGV,(void (*) (int))sigsegv_handler);
351  #ifdef SIGBUS
352  si_set_signal(SIGBUS, sigsegv_handler);
353  #endif /* SIGBUS */
354  #ifdef SIGFPE
355  si_set_signal(SIGFPE, sigsegv_handler);
356  #endif /* SIGFPE */
357  #ifdef SIGILL
358  si_set_signal(SIGILL, sigsegv_handler);
359  #endif /* SIGILL */
360  #ifdef SIGIOT
361  si_set_signal(SIGIOT, sigsegv_handler);
362  #endif /* SIGIOT */
363  #ifdef SIGXCPU
364  si_set_signal(SIGXCPU, (void (*)(int))SIG_IGN);
365  #endif /* SIGIOT */
366  si_set_signal(SIGINT ,sigint_handler);
367  #if defined(HPUX_9) || defined(HPUX_10)
368  si_set_signal(SIGCHLD, (void (*)(int))SIG_IGN);
369  #endif
370  si_set_signal(SIGPIPE, (si_hdl_typ)sig_pipe_hdl);
371}
372#endif
373
374
375/*2
376* signal handler for SIGINT
377*/
378void sigint_handler(int sig)
379{
380  mflush();
381  #ifdef HAVE_FEREAD
382  if (fe_is_raw_tty) fe_temp_reset();
383  #endif /* HAVE_FEREAD */
384  loop
385  {
386    int cnt=0;
387    int c;
388    fprintf(stderr,"// ** Interrupt at cmd:`%s` in line:'%s'\n",
389      Tok2Cmdname(iiOp),my_yylinebuf);
390    if (feGetOptValue(FE_OPT_EMACS) == NULL)
391    {
392      fputs("abort command(a), continue(c) or quit Singular(q) ?",stderr);fflush(stderr);
393      c = fgetc(stdin);
394    }
395    else
396    {
397      c = 'a';
398    }
399
400    switch(c)
401    {
402      case 'q':
403                m2_end(2);
404      case 'r':
405                longjmp(si_start_jmpbuf,1);
406      case 'b':
407                VoiceBackTrack();
408                break;
409      case 'a':
410                siCntrlc++;
411      case 'c':
412                if (feGetOptValue(FE_OPT_EMACS) == NULL) fgetc(stdin);
413                si_set_signal(SIGINT ,(si_hdl_typ)sigint_handler);
414                return;
415                //siCntrlc ++;
416                //if (siCntrlc>2) si_set_signal(SIGINT,(si_hdl_typ) sigsegv_handler);
417                //else            si_set_signal(SIGINT,(si_hdl_typ) sigint_handler);
418    }
419    cnt++;
420    if(cnt>5) m2_end(2);
421  }
422}
423
424//void test_int()
425//{
426//  if (siCntrlc!=0)
427//  {
428//    int saveecho = si_echo;
429//    siCntrlc = FALSE;
430//    si_set_signal(SIGINT ,sigint_handler);
431//    iiDebug();
432//    si_echo = saveecho;
433//  }
434//}
435
436#ifdef unix
437# ifndef hpux
438#  ifndef __OPTIMIZE__
439int si_stop_stack_trace_x;
440#    ifdef CALL_GDB
441static void debug (int method)
442{
443  if (feOptValue(FE_OPT_NO_TTY))
444  {
445    dReportError("Caught Signal 11");
446    return;
447  }
448  int pid;
449  char buf[16];
450  char * args[4] = { (char*)"gdb", (char*)"Singularg", NULL, NULL };
451
452  #ifdef HAVE_FEREAD
453  if (fe_is_raw_tty) fe_temp_reset();
454  #endif /* HAVE_FEREAD */
455
456  sprintf (buf, "%d", getpid ());
457
458  args[2] = buf;
459
460  pid = fork ();
461  if (pid == 0)
462  {
463    switch (method)
464    {
465      case INTERACTIVE:
466        fprintf (stderr, "debug_stop\n");
467        debug_stop (args);
468        break;
469      #ifndef __OPTIMIZE__
470      case STACK_TRACE:
471        fprintf (stderr, "stack_trace\n");
472        stack_trace (args);
473        break;
474      #endif
475      default:
476        // should not be reached:
477        exit(1);
478    }
479  }
480  else if (pid == -1)
481  {
482    perror ("could not fork");
483    return;
484  }
485
486  si_stop_stack_trace_x = 1;
487  while (si_stop_stack_trace_x) ;
488}
489
490static void debug_stop (char *const*args)
491{
492  execvp (args[0], args);
493  perror ("exec failed");
494  _exit (0);
495}
496#    endif /* CALL_GDB */
497
498static int stack_trace_done;
499
500static void stack_trace (char *const*args)
501{
502  int pid;
503  int in_fd[2];
504  int out_fd[2];
505  fd_set fdset;
506  fd_set readset;
507  struct timeval tv;
508  int sel, index, state;
509  char buffer[256];
510  char c;
511
512  stack_trace_done = 0;
513
514  signal (SIGCHLD, stack_trace_sigchld);
515
516  if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
517  {
518    perror ("could open pipe");
519    m2_end(999);
520  }
521
522  pid = fork ();
523  if (pid == 0)
524  {
525    close (0); dup2 (in_fd[0],0);   /* set the stdin to the in pipe */
526    close (1); dup2 (out_fd[1],1);  /* set the stdout to the out pipe */
527    close (2); dup2 (out_fd[1],2);  /* set the stderr to the out pipe */
528
529    execvp (args[0], args);      /* exec gdb */
530    perror ("exec failed");
531    m2_end(999);
532  }
533  else if (pid == -1)
534  {
535    perror ("could not fork");
536    m2_end(999);
537  }
538
539  FD_ZERO (&fdset);
540  FD_SET (out_fd[0], &fdset);
541
542  write (in_fd[1], "backtrace\n", 10);
543  write (in_fd[1], "p si_stop_stack_trace_x = 0\n", 28);
544  write (in_fd[1], "quit\n", 5);
545
546  index = 0;
547  state = 0;
548
549  loop
550  {
551    readset = fdset;
552    tv.tv_sec = 1;
553    tv.tv_usec = 0;
554
555#    ifdef hpux
556    sel = select (FD_SETSIZE, (int *)readset.fds_bits, NULL, NULL, &tv);
557#    else /* hpux */
558    sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
559#    endif /* hpux */
560    if (sel == -1)
561      break;
562
563    if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
564    {
565      if (read (out_fd[0], &c, 1))
566      {
567        switch (state)
568        {
569          case 0:
570            if (c == '#')
571            {
572              state = 1;
573              index = 0;
574              buffer[index++] = c;
575            }
576            break;
577          case 1:
578            buffer[index++] = c;
579            if ((c == '\n') || (c == '\r'))
580            {
581              buffer[index] = 0;
582              fprintf (stderr, "%s", buffer);
583              state = 0;
584              index = 0;
585            }
586            break;
587          default:
588            break;
589        }
590      }
591    }
592    else if (stack_trace_done)
593      break;
594  }
595
596  close (in_fd[0]);
597  close (in_fd[1]);
598  close (out_fd[0]);
599  close (out_fd[1]);
600  m2_end(0);
601}
602
603static void stack_trace_sigchld (int signum)
604{
605  stack_trace_done = 1;
606}
607
608#  endif /* !__OPTIMIZE__ */
609# endif /* !hpux */
610#endif /* unix */
611
612/* Under HPUX 9, system(...) returns -1 if SIGCHLD does not equal
613   SIG_DFL. However, if it stays at SIG_DFL we get zombie processes
614   for terminated childs generated by fork. Therefors some special treatment
615   is necessary */
616#ifdef HPUX_9
617# undef system
618extern "C" {
619  int  hpux9_system(const char* call)
620  {
621    int ret;
622    si_set_signal(SIGCHLD, (void (*)(int))SIG_DFL);
623    ret = system(call);
624    si_set_signal(SIGCHLD, (void (*)(int))SIG_IGN);
625    return ret;
626  }
627}
628#endif /* HPUX_9 */
Note: See TracBrowser for help on using the repository browser.