Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / gromacs / gmxlib / sighandler.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * GROwing Monsters And Cloning Shrimps
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "typedefs.h"
40 #include "gmx_fatal.h"
41 #include "sighandler.h"
42
43
44 const char *gmx_stop_cond_name[] =
45 {
46     "None",
47     "Stop at the next neighbor search step",
48     "Stop at the next step",
49     "Abort"
50 };
51
52 /* these do not neccesarily match the stop condition, but are
53    referred to in the signal handler. */
54 const char *gmx_signal_name[] =
55 {
56     "None",
57     "INT",
58     "TERM",
59     "second INT/TERM",
60     "remote INT/TERM",
61     "remote second INT/TERM",
62     "USR1",
63     "Abort"
64 };
65
66 static volatile sig_atomic_t stop_condition   = gmx_stop_cond_none;
67 static volatile sig_atomic_t last_signal_name = 0;
68
69 static volatile sig_atomic_t usr_condition = 0;
70
71 static void signal_handler(int n)
72 {
73     switch (n)
74     {
75 /* windows doesn't do SIGINT correctly according to ANSI (yes, signals are in
76    ANSI C89, and windows spawns a thread specifically to run the INT signal
77    handler), but that doesn't matter for a simple signal handler like this. */
78         case SIGTERM:
79         case SIGINT:
80             /* we explicitly set things up to allow this: */
81             stop_condition++;
82             if (n == SIGINT)
83             {
84                 last_signal_name = 1;
85             }
86             if (n == SIGTERM)
87             {
88                 last_signal_name = 2;
89             }
90             if (stop_condition == gmx_stop_cond_next)
91             {
92                 last_signal_name = 3;
93             }
94             if (stop_condition >= gmx_stop_cond_abort)
95             {
96                 abort();
97             }
98             break;
99 #ifdef HAVE_SIGUSR1
100         case SIGUSR1:
101             usr_condition = 1;
102             break;
103 #endif
104         default:
105             break;
106     }
107 }
108
109 static void gmx_signal(int signum)
110 {
111 #ifdef HAVE_SIGACTION
112     struct sigaction act;
113     act.sa_handler = signal_handler;
114     sigemptyset(&act.sa_mask);
115     act.sa_flags = SA_RESTART;
116     sigaction(signum, &act, NULL);
117 #else
118     signal(signum, signal_handler);
119 #endif
120 }
121
122 void signal_handler_install(void)
123 {
124     if (getenv("GMX_NO_TERM") == NULL)
125     {
126         if (debug)
127         {
128             fprintf(debug, "Installing signal handler for SIGTERM\n");
129         }
130         gmx_signal(SIGTERM);
131     }
132     if (getenv("GMX_NO_INT") == NULL)
133     {
134         if (debug)
135         {
136             fprintf(debug, "Installing signal handler for SIGINT\n");
137         }
138         gmx_signal(SIGINT);
139     }
140 #ifdef HAVE_SIGUSR1
141     if (getenv("GMX_NO_USR1") == NULL)
142     {
143         if (debug)
144         {
145             fprintf(debug, "Installing signal handler for SIGUSR1\n");
146         }
147         gmx_signal(SIGUSR1);
148     }
149 #endif
150 }
151
152 gmx_stop_cond_t gmx_get_stop_condition(void)
153 {
154     return (gmx_stop_cond_t)stop_condition;
155 }
156
157 void gmx_set_stop_condition(gmx_stop_cond_t recvd_stop_cond)
158 {
159     if (recvd_stop_cond > stop_condition)
160     {
161         stop_condition = recvd_stop_cond;
162         if (stop_condition == gmx_stop_cond_next_ns)
163         {
164             last_signal_name = 4;
165         }
166         if (stop_condition == gmx_stop_cond_next)
167         {
168             last_signal_name = 5;
169         }
170     }
171 }
172
173 const char *gmx_get_signal_name(void)
174 {
175     return gmx_signal_name[last_signal_name];
176 }
177
178 gmx_bool gmx_got_usr_signal(void)
179 {
180 #ifdef HAVE_SIGUSR1
181     gmx_bool ret = (gmx_bool)usr_condition;
182     usr_condition = 0;
183     return ret;
184 #else
185     return FALSE;
186 #endif
187 }