Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
interrupt.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include "extern.h"
60 
61 int control_c_hit = 0;
62 
63 extern tcflag_t old_lflag;
64 extern cc_t old_vtime;
65 extern struct termios term;
66 extern int history_already_written;
67 extern int lp_ppid;
68 extern int logging;
69 
70 
71 
78 void exit_proc(int arg1, void *arg2)
79 {
80  char sysbuf[256];
81 
83  {
85  }
86  else
87  {
88  st_report("Not Removing Ignored Lockfile: %s", lockFileName);
89  }
90 
91  if(xgraphic)
92  {
93  st_report("Killing Graphics Program");
94  sock_send(xgraphic, "exit");
95 
96  usleep(250000);
97  }
98 
99  if(helper)
100  {
101  st_report("Killing Helper Program");
102  sock_send(helper, "exit");
103 
104  usleep(250000);
105  }
106 
107  if(!no_terminal)
108  {
109  /* reset the old terminal setting before exiting */
110  term.c_lflag = old_lflag;
111  term.c_cc[VTIME] = old_vtime;
112 
113  if( tcsetattr(fileno(stdin), TCSANOW, &term) < 0)
114  {
115  perror("tcsetattr");
116  }
117  }
118 
119  printTimeStamp();
120 
121  /* Because this will kill this process, set a flag so I know to ignore */
123 
124  sprintf(sysbuf, "kill `ps ax | grep -- '-tn %d' | grep -v grep | awk '{print $1}'` 1>> /dev/null 2>&1", lp_ppid);
125  system(sysbuf);
126 
127  st_report("Killing any Remaining Processes belonging to PPID: %d", lp_ppid);
128  sprintf(sysbuf, "pkill -g %d", lp_ppid);
129  system(sysbuf);
130 
131  if(logging)
132  {
133  st_report("Closing logging file: %s", logfilename);
134  }
135 }
136 
137 
138 
139 
140 /* Any loop in lp that might take a long time needs to
141  * call this function first, and the call the isInterrupt()
142  * on a regular bassis to see if it time to break out of a loop.
143  */
145 {
146  control_c_hit = 0;
147 
148  return(0);
149 }
150 
151 
153 {
154  if(control_c_hit)
155  {
156  st_print("Interrupt Detected\n");
157  return(1);
158  }
159 
160  return(0);
161 }
162 
163 
168 void sig_handler(int sig)
169 {
170  time_t now;
171  static time_t old_time = 0;
172  char cmdBuf[256];
173 
174  if(sig == SIGTERM) /* External kill */
175  {
177  {
178  st_report("Saving History");
180  st_report("Saving Formulas");
181 
182  sprintf(cmdBuf, "formula /w .lp.history");
183  parseCmd(cmdBuf);
184 
186  }
187 
188  if(doingOTF)
189  {
190  st_report("Sending quit to dataserv");
191  sock_send(dataserv, "q");
192  doingOTF = 0;
193  }
194 
195  exit(1);
196  }
197 
198  now = time(NULL);
199 
200  if(now-old_time > 2)
201  {
202  st_print("Caught Signal %d\n", sig);
203  st_print("Hit <Control-C> within 2 Seconds to Exit\n");
204 
205  old_time = now;
206 
207  control_c_hit = 1;
208 
209  return;
210  }
211 
212  st_print("Caught 2nd signal: %d\n", sig);
213 
215  {
216  st_report("Saving History");
218 
219  sprintf(cmdBuf, "formula /w .lp.history");
220  parseCmd(cmdBuf);
221 
223  }
224 
225  if(doingOTF)
226  {
227  st_report("Sending quit to dataserv");
228  sock_send(dataserv, "q");
229  doingOTF = 0;
230  }
231 
232  exit(1);
233 }
234 
235 
236 
238 {
239  struct sigaction new_action, old_action;
240 
241  /* SIGNALS */
242 
243  /* Register exit_proc to remove lock-file */
244  on_exit(exit_proc, NULL);
245 
246  /* Set up the structure to specify the new action. */
247  new_action.sa_handler = sig_handler;
248  sigemptyset (&new_action.sa_mask);
249  new_action.sa_flags = 0;
250 
251  sigaction (SIGINT, NULL, &old_action);
252  if (old_action.sa_handler != SIG_IGN)
253  {
254  sigaction (SIGINT, &new_action, NULL);
255  }
256  else
257  {
258  st_fail("main(): Unable to setup signal(INT)");
259  }
260 
261  sigaction (SIGHUP, NULL, &old_action);
262  if (old_action.sa_handler != SIG_IGN)
263  {
264  sigaction (SIGHUP, &new_action, NULL);
265  }
266  else
267  {
268  st_fail("main(): Unable to setup signal(HUP)");
269  }
270 
271  sigaction (SIGTERM, NULL, &old_action);
272  if (old_action.sa_handler != SIG_IGN)
273  {
274  sigaction (SIGTERM, &new_action, NULL);
275  }
276  else
277  {
278  st_fail("main(): Unable to setup signal(TERM)");
279  }
280 
281  sigaction (SIGKILL, NULL, &old_action);
282  if (old_action.sa_handler != SIG_IGN)
283  {
284  sigaction (SIGKILL, &new_action, NULL);
285  }
286  else
287  {
288  st_fail("main(): Unable to setup signal(KILL)");
289  }
290 
291  sigaction (SIGQUIT, NULL, &old_action);
292  if (old_action.sa_handler != SIG_IGN)
293  {
294  sigaction (SIGQUIT, &new_action, NULL);
295  }
296  else
297  {
298  st_fail("main(): Unable to setup signal(QUIT)");
299  }
300 
301  return(0);
302 }
struct SOCK * dataserv
Definition: linuxpops.c:75
struct SOCK * xgraphic
Definition: linuxpops.c:75
int removeLockFile()
Definition: io.c:362
int history_already_written
Definition: linuxpops.c:90
void sig_handler(int sig)
Definition: interrupt.c:168
int rwHistory(int rw)
Definition: history.c:114
struct termios term
Definition: linuxpops.c:68
int control_c_hit
Definition: interrupt.c:61
int isInterrupt()
Definition: interrupt.c:152
#define WRITE_HISTORY
Definition: makemem.c:85
int clearInterrupt()
Definition: interrupt.c:144
struct SOCK * helper
Definition: linuxpops.c:75
int sock_send(struct SOCK *s, char *message)
int no_terminal
Definition: linuxpops.c:91
void st_fail()
tcflag_t old_lflag
Definition: linuxpops.c:66
int logging
Definition: linuxpops.c:77
int lp_ppid
Definition: linuxpops.c:89
void st_report()
char logfilename[256]
Definition: strings.c:67
cc_t old_vtime
Definition: linuxpops.c:67
char lockFileName[256]
Definition: makemem.c:97
int doNotRemoveLockFile
Definition: linuxpops.c:81
int initSignals()
Definition: interrupt.c:237
void st_print()
void exit_proc(int arg1, void *arg2)
Definition: interrupt.c:78
int parseCmd(char *buf)
Definition: parse.c:2090
int printTimeStamp()
Definition: linuxpops.c:1498
int doingOTF
Definition: otf.c:64