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