Revised wording in pdb2gmx.c, hopefully clearer now.
[gromacs/rigid-bodies.git] / src / gmxlib / sighandler.c
blob10e5e2d7250ca0d3a6d1c6e5459bd807e31c6e25
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.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROwing Monsters And Cloning Shrimps
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include "typedefs.h"
40 #include "gmx_fatal.h"
41 #include "sighandler.h"
44 const char *gmx_stop_cond_name[] =
46 "None",
47 "Stop at the next neighbor search step",
48 "Stop at the next step",
49 "Abort"
52 /* these do not neccesarily match the stop condition, but are
53 referred to in the signal handler. */
54 const char *gmx_signal_name[] =
56 "None",
57 "INT",
58 "TERM",
59 "second INT/TERM",
60 "remote INT/TERM",
61 "remote second INT/TERM",
62 "USR1",
63 "Abort"
66 static volatile sig_atomic_t stop_condition=gmx_stop_cond_none;
67 static volatile sig_atomic_t last_signal_name=0;
69 static volatile sig_atomic_t usr_condition=0;
71 static RETSIGTYPE signal_handler(int n)
73 switch (n) {
74 /* windows doesn't do SIGINT correctly according to ANSI (yes, signals are in
75 ANSI C89, and windows spawns a thread specifically to run the INT signal
76 handler), but that doesn't matter for a simple signal handler like this. */
77 case SIGTERM:
78 case SIGINT:
79 /* we explicitly set things up to allow this: */
80 stop_condition++;
81 if (n==SIGINT)
82 last_signal_name=1;
83 if (n==SIGTERM)
84 last_signal_name=2;
85 if (stop_condition == gmx_stop_cond_next)
86 last_signal_name=3;
87 if (stop_condition >= gmx_stop_cond_abort)
88 abort();
89 break;
90 #ifdef HAVE_SIGUSR1
91 case SIGUSR1:
92 usr_condition=1;
93 break;
94 #endif
95 default:
96 break;
101 void signal_handler_install(void)
103 if (getenv("GMX_NO_TERM") == NULL)
105 if (debug)
107 fprintf(debug,"Installing signal handler for SIGTERM\n");
109 signal(SIGTERM,signal_handler);
111 if (getenv("GMX_NO_INT") == NULL)
113 if (debug)
115 fprintf(debug,"Installing signal handler for SIGINT\n");
117 signal(SIGINT,signal_handler);
119 #ifdef HAVE_SIGUSR1
120 if (getenv("GMX_NO_USR1") == NULL)
122 if (debug)
124 fprintf(debug,"Installing signal handler for SIGUSR1\n");
126 signal(SIGUSR1,signal_handler);
128 #endif
131 gmx_stop_cond_t gmx_get_stop_condition(void)
133 return (gmx_stop_cond_t)stop_condition;
136 void gmx_set_stop_condition(gmx_stop_cond_t recvd_stop_cond)
138 if (recvd_stop_cond > stop_condition)
140 stop_condition=recvd_stop_cond;
141 if (stop_condition == gmx_stop_cond_next_ns)
142 last_signal_name=4;
143 if (stop_condition == gmx_stop_cond_next)
144 last_signal_name=5;
148 const char *gmx_get_signal_name(void)
150 return gmx_signal_name[last_signal_name];
153 gmx_bool gmx_got_usr_signal(void)
155 #ifdef HAVE_SIGUSR1
156 gmx_bool ret=(gmx_bool)usr_condition;
157 usr_condition=0;
158 return ret;
159 #else
160 return FALSE;
161 #endif