source: git/Singular/cntrlc.cc @ 3631a1

fieker-DuValspielwiese
Last change on this file since 3631a1 was 92644b, checked in by Hans Schoenemann <hannes@…>, 11 years ago
fix: version checks for header removed Singular/version.h (define now in config.h) new check for S_UNAME: uname -m uname -s remmoved feVersionId
  • Property mode set to 100644
File size: 14.1 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT - interupt handling
6*/
7
8/* includes */
[502ea2]9#ifdef DecAlpha_OSF1
10#define _XOPEN_SOURCE_EXTENDED
[d5bd816]11#endif /* MP3-Y2 0.022UF */
[ef6f95]12
[0e1846]13#include <stdio.h>
14#include <stddef.h>
15#include <stdlib.h>
[502ea2]16#include <strings.h>
[0e1846]17#include <signal.h>
[561aa2]18#include <sys/types.h>
19#include <sys/wait.h>
[ef6f95]20
[16f511]21#ifdef HAVE_CONFIG_H
[762407]22#include "config.h"
[16f511]23#endif /* HAVE_CONFIG_H */
[b1dfaf]24#include <kernel/mod2.h>
25#include <omalloc/omalloc.h>
[737a68]26
27#include <kernel/polys.h>
[599326]28#include <kernel/febase.h>
[751785e]29void sig_chld_hdl(int sig); /*#include <Singular/links/ssiLink.h>*/
[f8565a]30//#include <Singular/feOpt.h>
31//#include <Singular/silink.h>
32//#include <Singular/ssiLink.h>
33#include <Singular/si_signals.h>
[a40080]34
35#include "tok.h"
36#include "ipshell.h"
37#include "cntrlc.h"
38#include "feOpt.h"
[44ca2f]39#include "links/silink.h"
[dc0898]40
[d6681d]41/* undef, if you don't want GDB to come up on error */
[b39d4d]42
[d6681d]43#if defined(__OPTIMIZE__) && defined(CALL_GDB)
44#undef CALL_GDB
45#endif
46
[751785e]47#if defined(unix)
[337d5b]48 #include <unistd.h>
49 #include <sys/types.h>
50
51 #ifdef TIME_WITH_SYS_TIME
52   #include <time.h>
53   #ifdef HAVE_SYS_TIME_H
54     #include <sys/time.h>
55   #endif
56 #else
57   #ifdef HAVE_SYS_TIME_H
58     #include <sys/time.h>
59   #else
60     #include <time.h>
61   #endif
62 #endif
63 #ifdef HAVE_SYS_TIMES_H
64   #include <sys/times.h>
65 #endif
66
67 #define INTERACTIVE 0
68 #define STACK_TRACE 1
69
70 #ifdef CALL_GDB
71   static void debug (int);
[a5da6f]72   static void debug_stop (char *const*args);
[337d5b]73 #endif
74 #ifndef __OPTIMIZE__
75   static void stack_trace_sigchld (int);
[a5da6f]76   static void stack_trace (char *const*args);
[337d5b]77 #endif
78#endif
[0e1846]79
[85ab8cd]80si_link pipeLastLink=NULL;
[655e1f]81BOOLEAN singular_in_batchmode=FALSE;
[85ab8cd]82
[2e4ec14]83void sig_pipe_hdl(int /*sig*/)
[85ab8cd]84{
85 if (pipeLastLink!=NULL)
86 {
87   slClose(pipeLastLink);
88   pipeLastLink=NULL;
89   WerrorS("pipe failed");
90 }
91}
92
[2e4ec14]93void sig_term_hdl(int /*sig*/)
[7626c5f]94{
95 while (ssiToBeClosed!=NULL)
96 {
97   slClose(ssiToBeClosed->l);
[1da23e5]98   if (ssiToBeClosed==NULL) break;
[7626c5f]99   ssiToBeClosed=(link_list)ssiToBeClosed->next;
100 }
[387e4a]101 exit(1);
[7626c5f]102}
103
[d5bd816]104/*---------------------------------------------------------------------*
105 * File scope Variables (Variables share by several functions in
106 *                       the same file )
107 *
108 *---------------------------------------------------------------------*/
[0e1846]109/* data */
[07dacd]110jmp_buf si_start_jmpbuf;
[0e1846]111int siRandomStart;
[07dacd]112short si_restart=0;
113BOOLEAN siCntrlc = FALSE;
[0e1846]114
[5812c69]115typedef void (*si_hdl_typ)(int);
116
[7c96c03]117
[0e1846]118/*0 implementation*/
[d5bd816]119/*---------------------------------------------------------------------*
120 * Functions declarations
121 *
122 *---------------------------------------------------------------------*/
[0e1846]123void sigint_handler(int sig);
[d5bd816]124
[7c96c03]125si_hdl_typ si_set_signal ( int sig, si_hdl_typ signal_handler);
[d5bd816]126
127/*---------------------------------------------------------------------*/
[337d5b]128/**
[d5bd816]129 * @brief meta function for binding a signal to an handler
130
131 @param[in] sig             Signal number
132 @param[in] signal_handler  Pointer to signal handler
133
134 @return value of signal()
135**/
136/*---------------------------------------------------------------------*/
[85ab8cd]137si_hdl_typ si_set_signal ( int sig, si_hdl_typ signal_handler)
[d5bd816]138{
[72de5f]139#if 0
[7c96c03]140  si_hdl_typ retval=signal (sig, (si_hdl_typ)signal_handler);
141  if (retval == SIG_ERR)
142  {
[d5bd816]143     fprintf(stderr, "Unable to init signal %d ... exiting...\n", sig);
144  }
[bdda8c2]145  si_siginterrupt(sig, 0);
[72de5f]146  /*system calls will be restarted if interrupted by  the  specified
147   * signal sig.  This is the default behavior in Linux.
148   */
149#else
150  struct sigaction new_action,old_action;
151
152  /* Set up the structure to specify the new action. */
153  new_action.sa_handler = signal_handler;
154  if (sig==SIGINT)
155    sigemptyset (&new_action.sa_mask);
156  else
157    new_action.sa_flags = SA_RESTART;
158
[bdda8c2]159  int r=si_sigaction (sig, &new_action, &old_action);
[72de5f]160  si_hdl_typ retval=(si_hdl_typ)old_action.sa_handler;
161  if (r == -1)
162  {
163     fprintf(stderr, "Unable to init signal %d ... exiting...\n", sig);
164     retval=SIG_ERR;
165  }
[206aed]166#endif
[d5bd816]167  return retval;
[7c96c03]168}                               /* si_set_signal */
[d5bd816]169
[0e1846]170
[d5bd816]171/*---------------------------------------------------------------------*/
[337d5b]172#if defined(ix86_Linux)
[4f818c]173  #if !defined(HAVE_SIGCONTEXT) && !defined(HAVE_ASM_SIGCONTEXT_H)
[c7a653]174// we need the following structure sigcontext_struct.
175// if configure finds asm/singcontext.h we assume
176// that this file contains the structure and is included
177// via signal.h
[0e1846]178struct sigcontext_struct {
179        unsigned short gs, __gsh;
180        unsigned short fs, __fsh;
181        unsigned short es, __esh;
182        unsigned short ds, __dsh;
183        unsigned long edi;
184        unsigned long esi;
185        unsigned long ebp;
186        unsigned long esp;
187        unsigned long ebx;
188        unsigned long edx;
189        unsigned long ecx;
190        unsigned long eax;
191        unsigned long trapno;
192        unsigned long err;
193        unsigned long eip;
194        unsigned short cs, __csh;
195        unsigned long eflags;
196        unsigned long esp_at_signal;
197        unsigned short ss, __ssh;
198        unsigned long i387;
199        unsigned long oldmask;
200        unsigned long cr2;
201};
[337d5b]202#endif
[f72aa03]203#define HAVE_SIGSTRUCT
[0e1846]204typedef struct sigcontext_struct sigcontext;
[f72aa03]205#endif
[337d5b]206
207#if defined(x86_64_Linux)
[f72aa03]208#define HAVE_SIGSTRUCT
209#endif
[0e1846]210
[d5bd816]211
[337d5b]212#if defined(HAVE_SIGSTRUCT)
[d5bd816]213/*2---------------------------------------------------------------------*/
[337d5b]214/**
215 * @brief signal handler for run time errors, linux/i386, x86_64 version
[d5bd816]216
[337d5b]217 @param[in] sig
218 @param[in] s
[d5bd816]219**/
220/*---------------------------------------------------------------------*/
[0e1846]221void sigsegv_handler(int sig, sigcontext s)
222{
[92644b]223  fprintf(stderr,"Singular : signal %d (v: %d):\n",sig,SINGULAR_VERSION);
[0e1846]224  if (sig!=SIGINT)
225  {
[f9d901]226    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
[1270db]227    fprintf(stderr,"Segment fault/Bus error occurred at %lx because of %lx (r:%d)\n"
[0e1846]228                   "please inform the authors\n",
[c7a653]229                   #ifdef __i386__
[28fc7b]230                   (long)s.eip,
[c7a653]231                   #else /* x86_64*/
[28fc7b]232                   (long)s.rip,
[c7a653]233                   #endif
234                   (long)s.cr2,siRandomStart);
[0e1846]235  }
[337d5b]236#ifdef __OPTIMIZE__
[07dacd]237  if(si_restart<3)
238  {
239    si_restart++;
240    fprintf(stderr,"trying to restart...\n");
241    init_signals();
242    longjmp(si_start_jmpbuf,1);
243  }
[337d5b]244#endif /* __OPTIMIZE__ */
245#ifdef CALL_GDB
[788fdf]246  if (sig!=SIGINT)
[9803fb]247  {
248    if (singular_in_batchmode) debug(STACK_TRACE);
249    else                       debug(INTERACTIVE);
250  }
[337d5b]251#endif /* CALL_GDB */
[0e1846]252  exit(0);
253}
254
[d5bd816]255/*---------------------------------------------------------------------*/
[337d5b]256#elif defined(SunOS) /*SPARC_SUNOS*/
[0e1846]257/*2
258* signal handler for run time errors, sparc sunos 4 version
259*/
260void sigsegv_handler(int sig, int code, struct sigcontext *scp, char *addr)
261{
[92644b]262  fprintf(stderr,"Singular : signal %d, code %d (v: %d):\n",
263    sig,code,SINGULAR_VERSION);
[0e1846]264  if ((sig!=SIGINT)&&(sig!=SIGABRT))
265  {
[f9d901]266    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
[0e1846]267    fprintf(stderr,"Segment fault/Bus error occurred at %x (r:%d)\n"
268                   "please inform the authors\n",
269                   (int)addr,siRandomStart);
270  }
[337d5b]271#ifdef __OPTIMIZE__
[97eb91]272  if(si_restart<3)
273  {
274    si_restart++;
275    fprintf(stderr,"trying to restart...\n");
276    init_signals();
277    longjmp(si_start_jmpbuf,1);
278  }
[337d5b]279#endif /* __OPTIMIZE__ */
280#ifdef CALL_GDB
[0e1846]281  if (sig!=SIGINT) debug(STACK_TRACE);
[337d5b]282#endif /* CALL_GDB */
[0e1846]283  exit(0);
284}
[337d5b]285#else
[0e1846]286
[d5bd816]287/*---------------------------------------------------------------------*/
[0e1846]288/*2
289* signal handler for run time errors, general version
290*/
291void sigsegv_handler(int sig)
292{
[92644b]293  fprintf(stderr,"Singular : signal %d (v: %d):\n",
294    sig,SINGULAR_VERSION);
[0e1846]295  if (sig!=SIGINT)
296  {
[f9d901]297    fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
[0e1846]298    fprintf(stderr,"Segment fault/Bus error occurred (r:%d)\n"
299                   "please inform the authors\n",
300                   siRandomStart);
301  }
[337d5b]302  #ifdef __OPTIMIZE__
[97eb91]303  if(si_restart<3)
304  {
305    si_restart++;
306    fprintf(stderr,"trying to restart...\n");
307    init_signals();
308    longjmp(si_start_jmpbuf,1);
309  }
[337d5b]310  #endif /* __OPTIMIZE__ */
[751785e]311  #if defined(unix)
[337d5b]312  /* debug(..) does not work under HPUX (because ptrace does not work..) */
313  #ifdef CALL_GDB
[0e1846]314  if (sig!=SIGINT) debug(STACK_TRACE);
[337d5b]315  #endif /* CALL_GDB */
316  #endif /* unix */
[0e1846]317  exit(0);
318}
[337d5b]319#endif
[d5bd816]320
[0e1846]321/*2
322* signal handler for SIGINT
323*/
[32cb7a]324int sigint_handler_cnt=0;
[2e4ec14]325void sigint_handler(int /*sig*/)
[0e1846]326{
[66415e1]327  mflush();
[337d5b]328  #ifdef HAVE_FEREAD
[0fa9018]329  if (fe_is_raw_tty) fe_temp_reset();
[337d5b]330  #endif /* HAVE_FEREAD */
[32cb7a]331  char default_opt=' ';
332  if ((feOptSpec[FE_OPT_CNTRLC].value!=NULL)
333      && ((char*)(feOptSpec[FE_OPT_CNTRLC].value))[0])
334  { default_opt=((char*)(feOptSpec[FE_OPT_CNTRLC].value))[0]; }
[66415e1]335  loop
336  {
[e8a30d]337    int cnt=0;
[2272c33]338    int c;
[32cb7a]339
340    if (singular_in_batchmode)
[2272c33]341    {
[eed07e8]342      c = 'q';
[2272c33]343    }
[32cb7a]344    else if (default_opt!=' ')
345    {
346      c = default_opt;
347    }
[2272c33]348    else
349    {
[eed07e8]350      fprintf(stderr,"// ** Interrupt at cmd:`%s` in line:'%s'\n",
351        Tok2Cmdname(iiOp),my_yylinebuf);
352      if (feGetOptValue(FE_OPT_EMACS) == NULL)
353      {
[32cb7a]354        fputs("abort after this command(a), abort immediately(r), print backtrace(b), continue(c) or quit Singular(q) ?",stderr);fflush(stderr);
[eed07e8]355        c = fgetc(stdin);
356      }
357      else
358      {
359        c = 'a';
360      }
[2272c33]361    }
[337d5b]362
[2272c33]363    switch(c)
[66415e1]364    {
[32cb7a]365      case 'q': case EOF:
[66415e1]366                m2_end(2);
[07dacd]367      case 'r':
[32cb7a]368                if (sigint_handler_cnt<3)
369                {
370                  sigint_handler_cnt++;
371                  fputs("** Warning: Singular should be restarted as soon as possible **\n",stderr);
372                  fflush(stderr);
373                  longjmp(si_start_jmpbuf,1);
374                }
375                else
376                {
377                  fputs("** tried too often, try another possibility **\n",stderr);
378                  fflush(stderr);
379                }
380                break;
[09e228]381      case 'b':
382                VoiceBackTrack();
383                break;
[e8a30d]384      case 'a':
385                siCntrlc++;
[66415e1]386      case 'c':
[32cb7a]387                if ((feGetOptValue(FE_OPT_EMACS) == NULL) && (default_opt!=' '))
388                {
389                  /* Read until a newline or EOF */
390                  while (c != EOF && c != '\n') c = fgetc(stdin);
391                }
[7c96c03]392                si_set_signal(SIGINT ,(si_hdl_typ)sigint_handler);
[07dacd]393                return;
[50b2df]394                //siCntrlc ++;
[7c96c03]395                //if (siCntrlc>2) si_set_signal(SIGINT,(si_hdl_typ) sigsegv_handler);
396                //else            si_set_signal(SIGINT,(si_hdl_typ) sigint_handler);
[66415e1]397    }
[e8a30d]398    cnt++;
399    if(cnt>5) m2_end(2);
[66415e1]400  }
[0e1846]401}
402
[e8a30d]403//void test_int()
404//{
405//  if (siCntrlc!=0)
406//  {
407//    int saveecho = si_echo;
408//    siCntrlc = FALSE;
[7c96c03]409//    si_set_signal(SIGINT ,sigint_handler);
[e8a30d]410//    iiDebug();
411//    si_echo = saveecho;
412//  }
413//}
[0e1846]414
415#ifdef unix
[d5bd816]416#  ifndef __OPTIMIZE__
[ccae2f]417volatile int si_stop_stack_trace_x;
[d5bd816]418#    ifdef CALL_GDB
[577d19f]419static void debug (int method)
[0e1846]420{
[8a6c6a]421  if (feOptValue(FE_OPT_NO_TTY))
422  {
423    dReportError("Caught Signal 11");
424    return;
425  }
[0e1846]426  int pid;
427  char buf[16];
[a5da6f]428  char * args[4] = { (char*)"gdb", (char*)"Singularg", NULL, NULL };
[577d19f]429
[337d5b]430  #ifdef HAVE_FEREAD
[0fa9018]431  if (fe_is_raw_tty) fe_temp_reset();
[337d5b]432  #endif /* HAVE_FEREAD */
[0fa9018]433
[0e1846]434  sprintf (buf, "%d", getpid ());
435
436  args[2] = buf;
437
438  pid = fork ();
439  if (pid == 0)
440  {
441    switch (method)
442    {
443      case INTERACTIVE:
[ccae2f]444        fprintf (stderr, "\n\nquit with \"p si_stop_stack_trace_x=0\"\n\n\n");
[0e1846]445        debug_stop (args);
446        break;
447      case STACK_TRACE:
448        fprintf (stderr, "stack_trace\n");
449        stack_trace (args);
450        break;
[5687c9]451      default:
452        // should not be reached:
453        exit(1);
[0e1846]454    }
455  }
456  else if (pid == -1)
457  {
458    perror ("could not fork");
459    return;
460  }
461
462  si_stop_stack_trace_x = 1;
463  while (si_stop_stack_trace_x) ;
464}
465
[a5da6f]466static void debug_stop (char *const*args)
[0e1846]467{
468  execvp (args[0], args);
469  perror ("exec failed");
470  _exit (0);
471}
[d5bd816]472#    endif /* CALL_GDB */
[0e1846]473
[a5da6f]474static void stack_trace (char *const*args)
[0e1846]475{
476  int pid;
477  int in_fd[2];
478  int out_fd[2];
479  fd_set fdset;
480  fd_set readset;
481  struct timeval tv;
482  int sel, index, state;
483  char buffer[256];
484  char c;
485
486  if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
487  {
488    perror ("could open pipe");
[0fa9018]489    m2_end(999);
[0e1846]490  }
491
492  pid = fork ();
493  if (pid == 0)
494  {
[bdda8c2]495    si_close (0); si_dup2 (in_fd[0],0);   /* set the stdin to the in pipe */
496    si_close (1); si_dup2 (out_fd[1],1);  /* set the stdout to the out pipe */
497    si_close (2); si_dup2 (out_fd[1],2);  /* set the stderr to the out pipe */
[0e1846]498
499    execvp (args[0], args);      /* exec gdb */
500    perror ("exec failed");
[0fa9018]501    m2_end(999);
[0e1846]502  }
503  else if (pid == -1)
504  {
505    perror ("could not fork");
[0fa9018]506    m2_end(999);
[0e1846]507  }
508
509  FD_ZERO (&fdset);
510  FD_SET (out_fd[0], &fdset);
511
[bdda8c2]512  si_write (in_fd[1], "backtrace\n", 10);
513  si_write (in_fd[1], "p si_stop_stack_trace_x = 0\n", 28);
514  si_write (in_fd[1], "quit\n", 5);
[0e1846]515
516  index = 0;
517  state = 0;
518
519  loop
520  {
521    readset = fdset;
522    tv.tv_sec = 1;
523    tv.tv_usec = 0;
524
[bdda8c2]525    sel = si_select (FD_SETSIZE, &readset, NULL, NULL, &tv);
[0e1846]526    if (sel == -1)
527      break;
528
529    if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
530    {
[bdda8c2]531      if (si_read (out_fd[0], &c, 1))
[0e1846]532      {
533        switch (state)
534        {
535          case 0:
536            if (c == '#')
537            {
538              state = 1;
539              index = 0;
540              buffer[index++] = c;
541            }
542            break;
543          case 1:
544            buffer[index++] = c;
545            if ((c == '\n') || (c == '\r'))
546            {
547              buffer[index] = 0;
548              fprintf (stderr, "%s", buffer);
549              state = 0;
550              index = 0;
551            }
552            break;
553          default:
554            break;
555        }
556      }
557    }
[ccae2f]558    else if (si_stop_stack_trace_x==0)
[0e1846]559      break;
560  }
561
[bdda8c2]562  si_close (in_fd[0]);
563  si_close (in_fd[1]);
564  si_close (out_fd[0]);
565  si_close (out_fd[1]);
[0fa9018]566  m2_end(0);
[0e1846]567}
568
[d5bd816]569#  endif /* !__OPTIMIZE__ */
570#endif /* unix */
[dc0898]571
[751785e]572/*2
573* init signal handlers
574*/
575void init_signals()
576{
[60596c]577  #ifdef SIGSEGV
[751785e]578  si_set_signal(SIGSEGV,(si_hdl_typ)sigsegv_handler);
[60596c]579  #endif
580  #ifdef SIGBUS
581  si_set_signal(SIGBUS, (si_hdl_typ)sigsegv_handler);
582  #endif
583  #ifdef SIGFPE
[751785e]584  si_set_signal(SIGFPE, (si_hdl_typ)sigsegv_handler);
[60596c]585  #endif
586  #ifdef SIGILL
[751785e]587  si_set_signal(SIGILL, (si_hdl_typ)sigsegv_handler);
[60596c]588  #endif
589  #ifdef SIGIOT
[751785e]590  si_set_signal(SIGIOT, (si_hdl_typ)sigsegv_handler);
[60596c]591  #endif
[751785e]592  si_set_signal(SIGINT ,(si_hdl_typ)sigint_handler);
593  si_set_signal(SIGCHLD, (si_hdl_typ)sig_chld_hdl);
594  si_set_signal(SIGPIPE, (si_hdl_typ)sig_pipe_hdl);
595  si_set_signal(SIGTERM, (si_hdl_typ)sig_term_hdl);
[dc0898]596}
[751785e]597
Note: See TracBrowser for help on using the repository browser.