1
/*===========================================================================
2
Copyright (C) 1995-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
19
Corresponding concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
.AUTHOR IPG-ESO Garching
33
.CATEGORY Host operating system interfaces. Interprocess signals.
34
.COMMENTS These routines will handle the interprocess events and signals.
35
They will allow the following :
37
* to send signals to other processes
38
* to catch or to ignore these signals
39
* to do something when a signal is caught
40
* to handle system interrupts like arithmetic traps and so ...
41
* to suspend and resume processes after some time.
42
* to wait for a signal (process synchronization).
43
.VERSION [0.0] 08-Dec-1986 Definition B. Pirenne
44
.VERSION [0.1] 09-Dec-1986 Programmation B. Pirenne
45
.VERSION [0.2] 30-Jan-1987 Allow more signals to be catched B. Pirenne
46
.VERSION [0.3] 30-Jan-1987 Add a time out in osswait B. Pirenne
47
.VERSION [1.0] 07-Jul-1987 Add SIGINT to the allowed signals B. Pirenne.
48
.VERSION [1.1] 05-May-1987 Cosmetic changes C. Guirao.
49
.VERSION [1.2] 25-Apr-1991 VOID + signal.
50
.VERSION [2.1] 14-Jul-1992 VOID is called now VOIDSIG
51
.VERSION [3.1] 21-Apr-1993 Posix compliant
54
------------------------------------------------------------*/
56
* Define _POSIX_SOURCE to indicate
57
* that this is a POSIX program
59
#define _POSIX_SOURCE 1
66
#include <proto_os.h> /* ANSI-C prototyping */
68
#ifdef OSERROR_D /* oserror already defined (Silicon G)*/
69
#define oserror midaserror
75
typedef void (*FUNCTION)(); /* Pointer to Function */
79
static void alarm_handler(int sig)
81
static void alarm_handler(sig)
82
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
83
.PURPOSE dummy routine to catch SIGALRM avoiding killing the process
84
-----------------------------------------------------------*/
92
static void catch(int sig)
94
static void catch(sig)
95
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
96
.PURPOSE dummy routine to allow the pause call to return
97
.REMARKS ``sig'' is mandatory parameter always passed to a routine
99
.REMARKS sets the status variable to one if called.
100
------------------------------------------------------------------*/
108
int osssend(pid, sig)
109
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
110
.PURPOSE send a signal to a process
111
.RETURNS Upon successful completion a value of zero is returned, else 0
112
is returned and ``oserror'' is set to the actual error code.
113
.REMARKS System dependencies:
115
.REMARKS the allowed signals are only SIGUSR1,SIGUSR2,
117
------------------------------------------------------------*/
118
int pid; /* IN : process identification */
119
int sig; /* IN : signal (interruption) logical name */
126
case SIGALRM: /* allowed signals ... */
129
if(kill (pid,sig) < 0) {
134
default: /* not allowed signals */;
142
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
143
.PURPOSE Catch a signal and execute a given function when received.
144
.RETURNS Value 0 on normal return.
145
.REMARKS System dependencies:
147
.REMARKS This routine acts as a declarative statement : it means that
148
its effect continues until the end of the calling process or
149
until a new call to this routine for the same signal type.
150
It is then better located in the beginning of a program.
151
------------------------------------------------------------*/
152
int sig; /* IN : signal identification (see signal.h) */
153
FUNCTION f; /* IN : Function to call in case of Interrupt */
155
struct sigaction act, oact;
158
sigemptyset(&act.sa_mask);
160
if (sigaction(sig,&act,&oact) != 0) { oserror = errno; return(-1); };
166
int osswait(sig,time_out)
167
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
168
.PURPOSE Waits for signal to be received.
169
.RETURNS Value -1 if something wrong happened, 0 is a signal has been
170
received before the timeout, 1 if stopped for time out .
171
.REMARKS System dependencies:
172
--UNIX: sigaction(2), pause(2), sleep(3)
173
.REMARKS This routine acts as an executable statement : it means that
174
its effect will be to wait for the given signal (!! One can only
175
wait for a signal that it is possible to send (INT, ALRM, TERM, USR1
176
or USR2). The execution
177
of the calling process will be interrupted during this wait
178
time. The execution will continue if the given signal is received
179
or if the time out period is expired.
180
This routine is best used with ``osssend'' to suspend and resume
181
programmes execution.
182
.REMARKS It could have been implemented with sleep(). With Posix sleep should
183
have returned when any signal is received, but not with BSD systems
184
(SunOS) where sleep elapsed the time even when signals are received
185
------------------------------------------------------------*/
186
int sig; /* IN : signal identification (see signal.h) */
187
unsigned time_out; /* IN : amount of seconds allowed for the */
188
/* wait (0 = infinite) */
190
struct sigaction act, oact, oalrm;
194
/* check the value of the signal to wait for ... */
203
/* Catch the signal ALRM to avoid proccess to terminate */
204
act.sa_handler = alarm_handler;
205
sigemptyset(&act.sa_mask);
207
if (sigaction(SIGALRM,&act,&oalrm) != 0) { oserror = errno; return(-1); }
209
/* Catch the signal sig to change value of flag */
210
act.sa_handler = catch;
211
if (sigaction(sig,&act,&oact) != 0) { oserror = errno; return(-1); }
213
/* Suspend process all signal except sig & SIGALRM (for a */
216
sigdelset(&set,SIGALRM);
218
/* Set alarm if timeout */
219
if (time_out != 0) (void) alarm(time_out);
221
/* wait for signal to come or timeout */
222
(void) sigsuspend(&set);
224
/* If timeout cancel alarm */
225
if (time_out != 0) (void) alarm(0);
227
/* restore previous status for sig and SIGALRM */
228
if (sigaction(sig,&oact,&act) != 0) { oserror = errno; return(-1); }
229
if (sigaction(sig,&oalrm,&act) != 0) { oserror = errno; return(-1); }
230
return(flag); /* flag=0 (signal arrived), 1 (timeout first) */