My Project  debian-1:4.1.1-p2+ds-4build1
Macros | Typedefs | Functions | Variables
cntrlc.cc File Reference
#include "kernel/mod2.h"
#include "omalloc/omalloc.h"
#include "reporter/si_signals.h"
#include "Singular/fevoices.h"
#include "misc/options.h"
#include "Singular/tok.h"
#include "Singular/ipshell.h"
#include "Singular/cntrlc.h"
#include "Singular/feOpt.h"
#include "Singular/misc_ip.h"
#include "Singular/links/silink.h"
#include "Singular/links/ssiLink.h"
#include <NTL/version.h>
#include <NTL/tools.h>
#include <time.h>
#include <sys/time.h>

Go to the source code of this file.

Macros

#define CALL_GDB
 
#define INTERACTIVE   0
 
#define STACK_TRACE   1
 

Typedefs

typedef void(* si_hdl_typ) (int)
 

Functions

static void debug (int)
 
static void debug_stop (char *const *args)
 
static void stack_trace (char *const *args)
 
void sig_pipe_hdl (int)
 
void sig_term_hdl (int)
 
void sigint_handler (int)
 
si_hdl_typ si_set_signal (int sig, si_hdl_typ signal_handler)
 meta function for binding a signal to an handler More...
 
void sigsegv_handler (int sig)
 
void init_signals ()
 init signal handlers and error handling for libraries: NTL, factory More...
 

Variables

si_link pipeLastLink =NULL
 
BOOLEAN singular_in_batchmode =FALSE
 
volatile BOOLEAN do_shutdown = FALSE
 
volatile int defer_shutdown = 0
 
jmp_buf si_start_jmpbuf
 
int siRandomStart
 
short si_restart =0
 
int sigint_handler_cnt =0
 
volatile int si_stop_stack_trace_x
 

Macro Definition Documentation

◆ CALL_GDB

#define CALL_GDB

Definition at line 33 of file cntrlc.cc.

◆ INTERACTIVE

#define INTERACTIVE   0

Definition at line 55 of file cntrlc.cc.

◆ STACK_TRACE

#define STACK_TRACE   1

Definition at line 56 of file cntrlc.cc.

Typedef Documentation

◆ si_hdl_typ

typedef void(* si_hdl_typ) (int)

Definition at line 101 of file cntrlc.cc.

Function Documentation

◆ debug()

static void debug ( int  method)
static

Definition at line 408 of file cntrlc.cc.

409 {
410  if (feOptValue(FE_OPT_NO_TTY))
411  {
412  dReportError("Caught Signal 11");
413  return;
414  }
415  int pid;
416  char buf[16];
417  char * args[4] = { (char*)"gdb", (char*)"Singular", NULL, NULL };
418 
419  #ifdef HAVE_FEREAD
421  #endif /* HAVE_FEREAD */
422 
423  sprintf (buf, "%d", getpid ());
424 
425  args[2] = buf;
426 
427  pid = fork ();
428  if (pid == 0)
429  {
430  switch (method)
431  {
432  case INTERACTIVE:
433  fputs ("\n\nquit with \"p si_stop_stack_trace_x=0\"\n\n\n",stderr);
434  debug_stop (args);
435  break;
436  case STACK_TRACE:
437  fputs ("stack_trace\n",stderr);
438  stack_trace (args);
439  break;
440  default:
441  // should not be reached:
442  exit(1);
443  }
444  }
445  else if (pid == -1)
446  {
447  perror ("could not fork");
448  return;
449  }
450 
452  while (si_stop_stack_trace_x) ;
453 }

◆ debug_stop()

static void debug_stop ( char *const args)
static

Definition at line 455 of file cntrlc.cc.

456 {
457  execvp (args[0], args);
458  perror ("exec failed");
459  _exit (0);
460 }

◆ init_signals()

void init_signals ( )

init signal handlers and error handling for libraries: NTL, factory

Definition at line 561 of file cntrlc.cc.

562 {
563 // NTL error handling (>= 9.3.0) ----------------------------------------
564 #ifdef HAVE_NTL
565 #if (((NTL_MAJOR_VERSION==9)&&(NTL_MINOR_VERSION>=3))||(NTL_MAJOR_VERSION>=10))
566  ErrorMsgCallback=WerrorS;
567  ErrorCallback=HALT;
568 #endif
569 #endif
570 // factory error handling: -----------------------------------------------
572 
573 // signal handler -------------------------------------------------------
574  #ifdef SIGSEGV
576  #endif
577  #ifdef SIGBUS
579  #endif
580  #ifdef SIGFPE
582  #endif
583  #ifdef SIGILL
585  #endif
586  #ifdef SIGIOT
588  #endif
593 }

◆ si_set_signal()

si_hdl_typ si_set_signal ( int  sig,
si_hdl_typ  signal_handler 
)

meta function for binding a signal to an handler

Parameters
[in]sigSignal number
[in]signal_handlerPointer to signal handler
Returns
value of signal()

Definition at line 123 of file cntrlc.cc.

124 {
125 #if 0
126  si_hdl_typ retval=signal (sig, (si_hdl_typ)signal_handler);
127  if (retval == SIG_ERR)
128  {
129  fprintf(stderr, "Unable to init signal %d ... exiting...\n", sig);
130  }
131  si_siginterrupt(sig, 0);
132  /*system calls will be restarted if interrupted by the specified
133  * signal sig. This is the default behavior in Linux.
134  */
135 #else
136  struct sigaction new_action,old_action;
137  memset(&new_action, 0, sizeof(struct sigaction));
138 
139  /* Set up the structure to specify the new action. */
140  new_action.sa_handler = signal_handler;
141  if (sig==SIGINT)
142  sigemptyset (&new_action.sa_mask);
143  else
144  new_action.sa_flags = SA_RESTART;
145 
146  int r=si_sigaction (sig, &new_action, &old_action);
147  si_hdl_typ retval=(si_hdl_typ)old_action.sa_handler;
148  if (r == -1)
149  {
150  fprintf(stderr, "Unable to init signal %d ... exiting...\n", sig);
151  retval=SIG_ERR;
152  }
153 #endif
154  return retval;
155 } /* si_set_signal */

◆ sig_pipe_hdl()

void sig_pipe_hdl ( int  )

Definition at line 69 of file cntrlc.cc.

70 {
71  if (pipeLastLink!=NULL)
72  {
75  WerrorS("pipe failed");
76  }
77 }

◆ sig_term_hdl()

void sig_term_hdl ( int  )

Definition at line 82 of file cntrlc.cc.

83 {
84  do_shutdown = TRUE;
85  if (!defer_shutdown)
86  {
87  m2_end(1);
88  }
89 }

◆ sigint_handler()

void sigint_handler ( int  )

Definition at line 311 of file cntrlc.cc.

312 {
313  mflush();
314  #ifdef HAVE_FEREAD
316  #endif /* HAVE_FEREAD */
317  char default_opt=' ';
318  if ((feOptSpec[FE_OPT_CNTRLC].value!=NULL)
319  && ((char*)(feOptSpec[FE_OPT_CNTRLC].value))[0])
320  { default_opt=((char*)(feOptSpec[FE_OPT_CNTRLC].value))[0]; }
321  loop
322  {
323  int cnt=0;
324  int c;
325 
327  {
328  c = 'q';
329  }
330  else if (default_opt!=' ')
331  {
332  c = default_opt;
333  }
334  else
335  {
336  fprintf(stderr,"// ** Interrupt at cmd:`%s` in line:'%s'\n",
338  if (feOptValue(FE_OPT_EMACS) == NULL)
339  {
340  fputs("abort after this command(a), abort immediately(r), print backtrace(b), continue(c) or quit Singular(q) ?",stderr);
341  fflush(stderr);fflush(stdin);
342  c = fgetc(stdin);
343  }
344  else
345  {
346  c = 'a';
347  }
348  }
349 
350  switch(c)
351  {
352  case 'q': case EOF:
353  m2_end(2);
354  case 'r':
355  if (sigint_handler_cnt<3)
356  {
358  fputs("** Warning: Singular should be restarted as soon as possible **\n",stderr);
359  fflush(stderr);
360  extern void my_yy_flush();
361  my_yy_flush();
363  longjmp(si_start_jmpbuf,1);
364  }
365  else
366  {
367  fputs("** tried too often, try another possibility **\n",stderr);
368  fflush(stderr);
369  }
370  break;
371  case 'b':
372  VoiceBackTrack();
373  break;
374  case 'a':
375  siCntrlc++;
376  case 'c':
377  if ((feOptValue(FE_OPT_EMACS) == NULL) && (default_opt!=' '))
378  {
379  /* Read until a newline or EOF */
380  while (c != EOF && c != '\n') c = fgetc(stdin);
381  }
383  return;
384  //siCntrlc ++;
385  //if (siCntrlc>2) si_set_signal(SIGINT,(si_hdl_typ) sigsegv_handler);
386  //else si_set_signal(SIGINT,(si_hdl_typ) sigint_handler);
387  }
388  cnt++;
389  if(cnt>5) m2_end(2);
390  }
391 }

◆ sigsegv_handler()

void sigsegv_handler ( int  sig)

Definition at line 279 of file cntrlc.cc.

280 {
281  fprintf(stderr,"Singular : signal %d (v: %d):\n",
282  sig,SINGULAR_VERSION);
283  if (sig!=SIGINT)
284  {
285  fprintf(stderr,"current line:>>%s<<\n",my_yylinebuf);
286  fprintf(stderr,"Segment fault/Bus error occurred (r:%d)\n"
287  "please inform the authors\n",
288  siRandomStart);
289  }
290  #ifdef __OPTIMIZE__
291  if(si_restart<3)
292  {
293  si_restart++;
294  fputs("trying to restart...\n",stderr);
295  init_signals();
296  longjmp(si_start_jmpbuf,1);
297  }
298  #endif /* __OPTIMIZE__ */
299  #ifdef CALL_GDB
300  if (sig!=SIGINT) debug(STACK_TRACE);
301  #endif /* CALL_GDB */
302  exit(0);
303 }

◆ stack_trace()

static void stack_trace ( char *const args)
static

Definition at line 463 of file cntrlc.cc.

464 {
465  int pid;
466  int in_fd[2];
467  int out_fd[2];
468  fd_set fdset;
469  fd_set readset;
470  struct timeval tv;
471  int sel, index, state;
472  char buffer[256];
473  char c;
474 
475  if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
476  {
477  perror ("could open pipe");
478  m2_end(999);
479  }
480 
481  pid = fork ();
482  if (pid == 0)
483  {
484  si_close (0); si_dup2 (in_fd[0],0); /* set the stdin to the in pipe */
485  si_close (1); si_dup2 (out_fd[1],1); /* set the stdout to the out pipe */
486  si_close (2); si_dup2 (out_fd[1],2); /* set the stderr to the out pipe */
487 
488  execvp (args[0], args); /* exec gdb */
489  perror ("exec failed");
490  m2_end(999);
491  }
492  else if (pid == -1)
493  {
494  perror ("could not fork");
495  m2_end(999);
496  }
497 
498  FD_ZERO (&fdset);
499  FD_SET (out_fd[0], &fdset);
500 
501  si_write (in_fd[1], "backtrace\n", 10);
502  si_write (in_fd[1], "p si_stop_stack_trace_x = 0\n", 28);
503  si_write (in_fd[1], "quit\n", 5);
504 
505  index = 0;
506  state = 0;
507 
508  loop
509  {
510  readset = fdset;
511  tv.tv_sec = 1;
512  tv.tv_usec = 0;
513 
514  sel = si_select (FD_SETSIZE, &readset, NULL, NULL, &tv);
515  if (sel == -1)
516  break;
517 
518  if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
519  {
520  if (si_read (out_fd[0], &c, 1))
521  {
522  switch (state)
523  {
524  case 0:
525  if (c == '#')
526  {
527  state = 1;
528  index = 0;
529  buffer[index++] = c;
530  }
531  break;
532  case 1:
533  buffer[index++] = c;
534  if ((c == '\n') || (c == '\r'))
535  {
536  buffer[index] = 0;
537  fputs (buffer,stderr);
538  state = 0;
539  index = 0;
540  }
541  break;
542  default:
543  break;
544  }
545  }
546  }
547  else if (si_stop_stack_trace_x==0)
548  break;
549  }
550 
551  si_close (in_fd[0]);
552  si_close (in_fd[1]);
553  si_close (out_fd[0]);
554  si_close (out_fd[1]);
555  m2_end(0);
556 }

Variable Documentation

◆ defer_shutdown

volatile int defer_shutdown = 0

Definition at line 80 of file cntrlc.cc.

◆ do_shutdown

volatile BOOLEAN do_shutdown = FALSE

Definition at line 79 of file cntrlc.cc.

◆ pipeLastLink

si_link pipeLastLink =NULL

Definition at line 66 of file cntrlc.cc.

◆ si_restart

short si_restart =0

Definition at line 99 of file cntrlc.cc.

◆ si_start_jmpbuf

jmp_buf si_start_jmpbuf

Definition at line 97 of file cntrlc.cc.

◆ si_stop_stack_trace_x

volatile int si_stop_stack_trace_x

Definition at line 406 of file cntrlc.cc.

◆ sigint_handler_cnt

int sigint_handler_cnt =0

Definition at line 310 of file cntrlc.cc.

◆ singular_in_batchmode

BOOLEAN singular_in_batchmode =FALSE

Definition at line 67 of file cntrlc.cc.

◆ siRandomStart

int siRandomStart

Definition at line 98 of file cntrlc.cc.

sigint_handler
void sigint_handler(int)
Definition: cntrlc.cc:311
dReportError
int dReportError(const char *fmt,...)
Definition: dError.cc:43
debug
static void debug(int)
Definition: cntrlc.cc:408
my_yy_flush
void my_yy_flush()
Definition: scanner.cc:2336
iiOp
int iiOp
Definition: iparith.cc:218
pipeLastLink
si_link pipeLastLink
Definition: cntrlc.cc:66
fe_is_raw_tty
BOOLEAN fe_is_raw_tty
Definition: fereadl.c:70
siRandomStart
int siRandomStart
Definition: cntrlc.cc:98
STACK_TRACE
#define STACK_TRACE
Definition: cntrlc.cc:56
loop
#define loop
Definition: structs.h:77
m2_end
void m2_end(int i)
Definition: misc_ip.cc:1099
si_siginterrupt
#define si_siginterrupt(arg1, arg2)
debug_stop
static void debug_stop(char *const *args)
Definition: cntrlc.cc:455
singular_in_batchmode
BOOLEAN singular_in_batchmode
Definition: cntrlc.cc:67
sig_term_hdl
void sig_term_hdl(int)
Definition: cntrlc.cc:82
do_shutdown
volatile BOOLEAN do_shutdown
Definition: cntrlc.cc:79
TRUE
#define TRUE
Definition: auxiliary.h:98
buf
int status int void * buf
Definition: si_signals.h:58
si_hdl_typ
void(* si_hdl_typ)(int)
Definition: cntrlc.cc:101
currentVoice
Voice * currentVoice
Definition: fevoices.cc:46
siCntrlc
BOOLEAN siCntrlc
Definition: options.c:14
SINGULAR_VERSION
#define SINGULAR_VERSION
Definition: mod2.h:85
sigint_handler_cnt
int sigint_handler_cnt
Definition: cntrlc.cc:310
VoiceBackTrack
void VoiceBackTrack()
Definition: fevoices.cc:66
my_yylinebuf
char my_yylinebuf[80]
Definition: febase.cc:42
si_set_signal
si_hdl_typ si_set_signal(int sig, si_hdl_typ signal_handler)
meta function for binding a signal to an handler
Definition: cntrlc.cc:123
init_signals
void init_signals()
init signal handlers and error handling for libraries: NTL, factory
Definition: cntrlc.cc:561
sigsegv_handler
void sigsegv_handler(int sig)
Definition: cntrlc.cc:279
HALT
static void HALT()
Definition: mod2.h:123
si_stop_stack_trace_x
volatile int si_stop_stack_trace_x
Definition: cntrlc.cc:406
feInitStdin
Voice * feInitStdin(Voice *pp)
Definition: fevoices.cc:654
stack_trace
static void stack_trace(char *const *args)
Definition: cntrlc.cc:463
feOptSpec
struct fe_option feOptSpec[]
factoryError
void(* factoryError)(const char *s)
Definition: cf_util.cc:75
sig_pipe_hdl
void sig_pipe_hdl(int)
Definition: cntrlc.cc:69
defer_shutdown
volatile int defer_shutdown
Definition: cntrlc.cc:80
si_restart
short si_restart
Definition: cntrlc.cc:99
INTERACTIVE
#define INTERACTIVE
Definition: cntrlc.cc:55
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
NULL
#define NULL
Definition: omList.c:9
fe_temp_reset
void fe_temp_reset(void)
Definition: fereadl.c:108
mflush
#define mflush()
Definition: reporter.h:56
Tok2Cmdname
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:137
feOptValue
static void * feOptValue(feOptIndex opt)
Definition: feOpt.h:39
si_start_jmpbuf
jmp_buf si_start_jmpbuf
Definition: cntrlc.cc:97
index
static int index(p_Length length, p_Ord ord)
Definition: p_Procs_Impl.h:585
if
if(yy_init)
Definition: libparse.cc:1417