~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to libsrc/os/unix/oss.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1995-2009 European Southern Observatory (ESO)
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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, 
 
17
  MA 02139, USA.
 
18
 
 
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 
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
29
.TYPE        Module
 
30
.NAME        oss
 
31
.LANGUAGE    C
 
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 :
 
36
                 * to kill processes
 
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
 
52
 
 
53
 090324         last modif
 
54
------------------------------------------------------------*/
 
55
/*
 
56
 * Define _POSIX_SOURCE to indicate
 
57
 * that this is a POSIX program
 
58
 */
 
59
#define _POSIX_SOURCE 1
 
60
 
 
61
#include <fcntl.h>
 
62
#include <stdio.h>
 
63
#include <signal.h>
 
64
#include <errno.h>
 
65
#include <unistd.h>
 
66
#include <proto_os.h>           /* ANSI-C prototyping */
 
67
 
 
68
#ifdef OSERROR_D                        /* oserror already defined (Silicon G)*/
 
69
#define oserror midaserror
 
70
#endif
 
71
 
 
72
extern int oserror;
 
73
static int flag;
 
74
 
 
75
typedef void (*FUNCTION)();     /* Pointer to Function  */
 
76
 
 
77
/* ARGSUSED */
 
78
#ifdef __STDC__
 
79
static void alarm_handler(int sig) 
 
80
#else
 
81
static void alarm_handler(sig) 
 
82
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
83
.PURPOSE dummy routine to catch SIGALRM avoiding killing the process 
 
84
-----------------------------------------------------------*/
 
85
int sig;
 
86
#endif
 
87
{
 
88
 
89
 
 
90
/* ARGSUSED */
 
91
#ifdef __STDC__
 
92
static void catch(int sig)
 
93
#else
 
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
 
98
         called from osscatch.
 
99
.REMARKS sets the status variable to one if called.
 
100
------------------------------------------------------------------*/
 
101
int sig;
 
102
#endif
 
103
{
 
104
  flag = 0;
 
105
}
 
106
 
 
107
 
 
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:
 
114
 --UNIX: kill(2)
 
115
.REMARKS the allowed signals are only SIGUSR1,SIGUSR2,
 
116
SIGTERM and SIGALRM.
 
117
------------------------------------------------------------*/
 
118
int  pid;               /* IN : process identification */
 
119
int  sig;               /* IN : signal (interruption) logical name */
 
120
{
 
121
  switch (sig) {
 
122
  case SIGKILL:
 
123
  case SIGUSR1:
 
124
  case SIGUSR2:
 
125
  case SIGTERM:
 
126
  case SIGALRM:      /* allowed signals ... */
 
127
  case SIGINT:
 
128
  case SIGCHLD:
 
129
    if(kill (pid,sig) < 0) {
 
130
      oserror = errno;
 
131
      return(-1);
 
132
      }
 
133
    break;
 
134
  default:           /* not allowed signals */;
 
135
    oserror = EINVAL;
 
136
    return(-1);
 
137
    }
 
138
  return(0);
 
139
}
 
140
 
 
141
int osscatch(sig, f)
 
142
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
143
.PURPOSE  Catch a signal and execute a given function when received.
 
144
.RETURNS Value 0 on normal return. 
 
145
.REMARKS System dependencies:
 
146
 --UNIX: sigaction()
 
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 */
 
154
{
 
155
  struct sigaction act, oact;
 
156
 
 
157
  act.sa_handler = f;
 
158
  sigemptyset(&act.sa_mask);
 
159
  act.sa_flags = 0;
 
160
  if (sigaction(sig,&act,&oact) != 0) { oserror = errno; return(-1); };
 
161
 
 
162
  return(0);
 
163
}
 
164
 
 
165
 
 
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) */
 
189
{
 
190
  struct sigaction act, oact, oalrm;
 
191
  sigset_t set;
 
192
        
 
193
  flag=1;
 
194
  /* check the value of the signal to wait for ... */
 
195
  switch (sig) {
 
196
  case SIGUSR1:
 
197
  case SIGUSR2:
 
198
  case SIGTERM:
 
199
  case SIGALRM:
 
200
  case SIGINT:
 
201
  case SIGCHLD:
 
202
 
 
203
    /* Catch the signal ALRM to avoid proccess to terminate */
 
204
    act.sa_handler = alarm_handler;
 
205
    sigemptyset(&act.sa_mask);
 
206
    act.sa_flags = 0;
 
207
    if (sigaction(SIGALRM,&act,&oalrm) != 0) { oserror = errno; return(-1); }
 
208
 
 
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); }
 
212
 
 
213
    /* Suspend process all signal except sig & SIGALRM (for a */
 
214
    sigfillset(&set);
 
215
    sigdelset(&set,sig);
 
216
    sigdelset(&set,SIGALRM);
 
217
 
 
218
    /* Set alarm if timeout */
 
219
    if (time_out != 0) (void) alarm(time_out);
 
220
 
 
221
    /*  wait for signal to come  or timeout */
 
222
    (void) sigsuspend(&set);
 
223
    
 
224
    /* If timeout cancel alarm */
 
225
    if (time_out != 0) (void) alarm(0);
 
226
 
 
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) */
 
231
  default:
 
232
    oserror = EINVAL;
 
233
    return(-1);
 
234
  }
 
235
}