PIPS
signal.c
Go to the documentation of this file.
1 /*
2 
3  $Id: signal.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /*
28  * signal management for pips.
29  * moved from misc to top level so as to interact with pipsmake/pipsdbm...
30  */
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <unistd.h>
36 
37 #include "genC.h"
38 #include "linear.h"
39 #include "ri.h"
40 #include "misc.h"
41 #include "pipsdbm.h"
42 #include "pipsmake.h"
43 #include "top-level.h"
44 
45 /************************************************************** PIPS TIMEOUT */
46 
48  const int delay,
49  _UNUSED_ const char * function,
50  const char * file,
51  const int lineno)
52 {
53  // nothing fancy can be done on this interrupt...
54  pips_log_stop(__func__, file, lineno, user_error_log,
55  TIMEOUT_CODE, "timeout after %d seconds", delay);
56 }
57 
58 static bool pips_timeout_set = false;
59 
61 {
62  if (pips_timeout_set)
63  POP_TIMEOUT();
64  pips_timeout_set = false;
65 }
66 
67 /* set pips timeout on delay
68  * setting to 0 removes the current timeout.
69  */
70 void set_pips_timeout(unsigned int delay)
71 {
74 
75  if (delay > 0)
76  {
77  pips_timeout_set = true;
78  PUSH_TIMEOUT(delay);
79  }
80 }
81 
82 /* set pips timeout using PIPS_TIMEOUT environment variable
83  */
85 {
88 
89  pips_timeout_set = PUSH_TIMEOUT_ENV("PIPS_TIMEOUT");
90 }
91 
92 /********************************************************* ALL OTHER SIGNALS */
93 
94 static void pips_signal_handler(int num)
95 {
96  user_log("interruption signal %d caught!\n", num);
97 
98  switch (num)
99  {
100  case SIGINT:
101  case SIGHUP:
102  case SIGTERM:
103  // BUG: if nothing is in progress (eg tpips prompt), should exit
104  user_log("interrupting pipsmake as soon as possible...\n");
106  break;
107  case SIGUSR1:
108  user_log("interruption for checkpointing...\n");
109  // cold blooded: might be quite dangerous for the life of the process.
110  // should not enter twice in there... might be convenient anyway.
112  break;
113  case SIGUSR2:
114  user_log("interruption for exiting...\n");
115  exit(128 + SIGUSR2);
116  break;
117  default:
118  fprintf(stderr, "[pips_signal_handler] unexpected signal %d\n", num);
119  abort();
120  }
121 
122  // must reset handler once raised.
123  (void) signal(num, pips_signal_handler);
124 }
125 
127 {
128  // misc signals
129  (void) signal(SIGINT, pips_signal_handler);
130  (void) signal(SIGHUP, pips_signal_handler);
131  (void) signal(SIGTERM, pips_signal_handler);
132 
133  (void) signal(SIGUSR1, pips_signal_handler);
134  (void) signal(SIGUSR2, pips_signal_handler);
135 
136  // timeout handling
138 }
void user_log(const char *format,...)
Definition: message.c:234
void pips_log_stop(const char *function, const char *file, const int lineno, const pips_log_t tag, const int code, const string format,...)
quick log and stop, called on timeout this must not invoke malloc as it may still held locks if inter...
Definition: message.c:1323
#define POP_TIMEOUT()
#define PUSH_TIMEOUT_ENV(env)
#define PUSH_TIMEOUT(delay)
void set_timeout_callback(timeout_callback_f)
Definition: errors.c:485
static int num
Definition: bourdoncle.c:137
#define _UNUSED_
Definition: misc-local.h:232
#define TIMEOUT_CODE
Definition: misc-local.h:43
@ user_error_log
Definition: misc-local.h:37
#define exit(code)
Definition: misc-local.h:54
#define abort()
Definition: misc-local.h:53
void checkpoint_workspace(void)
checkpoint the current workspace, i.e.
Definition: openclose.c:129
void interrupt_pipsmake_asap()
misc.c
Definition: misc.c:48
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
void initialize_signal_catcher(void)
Definition: signal.c:126
static void pips_timeout_callback(const int delay, _UNUSED_ const char *function, const char *file, const int lineno)
Definition: signal.c:47
void reset_pips_timeout(void)
signal.c
Definition: signal.c:60
static bool pips_timeout_set
Definition: signal.c:58
void set_pips_timeout_from_env(void)
set pips timeout using PIPS_TIMEOUT environment variable
Definition: signal.c:84
void set_pips_timeout(unsigned int delay)
set pips timeout on delay setting to 0 removes the current timeout.
Definition: signal.c:70
static void pips_signal_handler(int num)
Definition: signal.c:94