2149787465d6d5e961d67353cce18537cfc5d99d
[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 "gmxpre.h"
38
39 #include "config.h"
40
41 #include <stdlib.h>
42
43 #include "gromacs/legacyheaders/typedefs.h"
44 #include "gromacs/utility/fatalerror.h"
45 #include "gromacs/legacyheaders/sighandler.h"
46
47 const char *gmx_stop_cond_name[] =
48 {
49     "None",
50     "Stop at the next neighbor search step",
51     "Stop at the next step",
52     "Abort"
53 };
54
55 /* these do not neccesarily match the stop condition, but are
56    referred to in the signal handler. */
57 const char *gmx_signal_name[] =
58 {
59     "None",
60     "INT",
61     "TERM",
62     "second INT/TERM",
63     "remote INT/TERM",
64     "remote second INT/TERM",
65     "USR1",
66     "Abort"
67 };
68
69 static volatile sig_atomic_t stop_condition   = gmx_stop_cond_none;
70 static volatile sig_atomic_t last_signal_name = 0;
71
72 static volatile sig_atomic_t usr_condition = 0;
73
74 static void signal_handler(int n)
75 {
76     switch (n)
77     {
78 /* windows doesn't do SIGINT correctly according to ANSI (yes, signals are in
79    ANSI C89, and windows spawns a thread specifically to run the INT signal
80    handler), but that doesn't matter for a simple signal handler like this. */
81         case SIGTERM:
82         case SIGINT:
83             /* we explicitly set things up to allow this: */
84             stop_condition++;
85             if (n == SIGINT)
86             {
87                 last_signal_name = 1;
88             }
89             if (n == SIGTERM)
90             {
91                 last_signal_name = 2;
92             }
93             if (stop_condition == gmx_stop_cond_next)
94             {
95                 last_signal_name = 3;
96             }
97             if (stop_condition >= gmx_stop_cond_abort)
98             {
99                 abort();
100             }
101             break;
102 #ifdef HAVE_SIGUSR1
103         case SIGUSR1:
104             usr_condition = 1;
105             break;
106 #endif
107         default:
108             break;
109     }
110 }
111
112 static void gmx_signal(int signum)
113 {
114 #ifdef HAVE_SIGACTION
115     struct sigaction act;
116     act.sa_handler = signal_handler;
117     sigemptyset(&act.sa_mask);
118     act.sa_flags = SA_RESTART;
119     sigaction(signum, &act, NULL);
120 #else
121     signal(signum, signal_handler);
122 #endif
123 }
124
125 void signal_handler_install(void)
126 {
127     if (getenv("GMX_NO_TERM") == NULL)
128     {
129         if (debug)
130         {
131             fprintf(debug, "Installing signal handler for SIGTERM\n");
132         }
133         gmx_signal(SIGTERM);
134     }
135     if (getenv("GMX_NO_INT") == NULL)
136     {
137         if (debug)
138         {
139             fprintf(debug, "Installing signal handler for SIGINT\n");
140         }
141         gmx_signal(SIGINT);
142     }
143 #ifdef HAVE_SIGUSR1
144     if (getenv("GMX_NO_USR1") == NULL)
145     {
146         if (debug)
147         {
148             fprintf(debug, "Installing signal handler for SIGUSR1\n");
149         }
150         gmx_signal(SIGUSR1);
151     }
152 #endif
153 }
154
155 gmx_stop_cond_t gmx_get_stop_condition(void)
156 {
157     return (gmx_stop_cond_t)stop_condition;
158 }
159
160 void gmx_set_stop_condition(gmx_stop_cond_t recvd_stop_cond)
161 {
162     if (recvd_stop_cond > stop_condition)
163     {
164         stop_condition = recvd_stop_cond;
165         if (stop_condition == gmx_stop_cond_next_ns)
166         {
167             last_signal_name = 4;
168         }
169         if (stop_condition == gmx_stop_cond_next)
170         {
171             last_signal_name = 5;
172         }
173     }
174 }
175
176 const char *gmx_get_signal_name(void)
177 {
178     return gmx_signal_name[last_signal_name];
179 }
180
181 gmx_bool gmx_got_usr_signal(void)
182 {
183 #ifdef HAVE_SIGUSR1
184     gmx_bool ret = (gmx_bool)usr_condition;
185     usr_condition = 0;
186     return ret;
187 #else
188     return FALSE;
189 #endif
190 }