OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [signal/] [signal.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
2
FUNCTION
3
<<signal>>---specify handler subroutine for a signal
4
 
5
INDEX
6
        signal
7
INDEX
8
        _signal_r
9
INDEX
10
        raise
11
INDEX
12
        _raise_r
13
 
14
ANSI_SYNOPSIS
15
        #include <signal.h>
16
        void ( * signal(int <[sig]>, void(*<[func]>)(int)) )(int);
17
 
18
        void ( * _signal_r(void *<[reent]>,
19
                           int <[sig]>, void(*<[func]>)(int)) )(int);
20
 
21
        int raise (int <[sig]>);
22
 
23
        int _raise_r (void *<[reent]>, int <[sig]>);
24
 
25
TRAD_SYNOPSIS
26
        #include <signal.h>
27
        char ( * signal(<[sig]>, <[func]>) )()
28
        int <[sig]>;
29
        char ( * <[func]> )();
30
 
31
        char ( * _signal_r(<[reent]>, <[sig]>, <[func]>) )()
32
        char *<[reent]>;
33
        int <[sig]>;
34
        char ( * <[func]> )();
35
 
36
        int raise (<[sig]>)()
37
        int <[sig]>;
38
 
39
        int _raise_r (<[reent]>, <[sig]>)()
40
        char *<[reent]>;
41
        int <[sig]>;
42
 
43
DESCRIPTION
44
<<signal, raise>> provide a simple signal/raise implementation for embedded
45
targets.
46
 
47
<<signal>> allows you to request changed treatment for a particular
48
signal <[sig]>.  You can use one of the predefined macros <<SIG_DFL>>
49
(select system default handling) or <<SIG_IGN>> (ignore this signal)
50
as the value of <[func]>; otherwise, <[func]> is a function pointer
51
that identifies a subroutine in your program as the handler for this signal.
52
 
53
Some of the execution environment for signal handlers is
54
unpredictable; notably, the only library function required to work
55
correctly from within a signal handler is @code{signal} itself, and
56
only when used to redefine the handler for the current signal value.
57
 
58
Static storage is likewise unreliable for signal handlers, with one
59
exception: if you declare a static storage location as `<<volatile
60
sig_atomic_t>>', then you may use that location in a signal handler to
61
store signal values.
62
 
63
If your signal handler terminates using <<return>> (or implicit
64
return), your program's execution continues at the point
65
where it was when the signal was raised (whether by your program
66
itself, or by an external event).  Signal handlers can also
67
use functions such as <<exit>> and <<abort>> to avoid returning.
68
 
69
<<raise>> sends the signal sig to the executing program.  It returns zero if
70
successful, non-zero if unsuccessful.
71
 
72
The alternate functions <<_signal_r, _raise_r>> are the reentrant versions.
73
The extra argument <[reent]> is a pointer to a reentrancy structure.
74
 
75
 
76
@c FIXME: do we have setjmp.h and assoc fns?
77
 
78
RETURNS
79
If your request for a signal handler cannot be honored, the result is
80
<<SIG_ERR>>; a specific error number is also recorded in <<errno>>.
81
 
82
Otherwise, the result is the previous handler (a function pointer or
83
one of the predefined macros).
84
 
85
PORTABILITY
86
ANSI C requires <<raise>>, <<signal>>.
87
 
88
No supporting OS subroutines are required to link with <<signal>>, but
89
it will not have any useful effects, except for software generated signals,
90
without an operating system that can actually raise exceptions.
91
*/
92
 
93
/*
94
 * signal.c
95
 * Original Author:     G. Haley
96
 *
97
 * signal associates the function pointed to by func with the signal sig. When
98
 * a signal occurs, the value of func determines the action taken as follows:
99
 * if func is SIG_DFL, the default handling for that signal will occur; if func
100
 * is SIG_IGN, the signal will be ignored; otherwise, the default handling for
101
 * the signal is restored (SIG_DFL), and the function func is called with sig
102
 * as its argument. Returns the value of func for the previous call to signal
103
 * for the signal sig, or SIG_ERR if the request fails.
104
 */
105
 
106
/* _init_signal initialises the signal handlers for each signal. This function
107
   is called by crt0 at program startup.  */
108
 
109
#ifdef SIGNAL_PROVIDED
110
 
111
int _dummy_simulated_signal;
112
 
113
#else
114
 
115
#include <errno.h>
116
#include <signal.h>
117
#include <stddef.h>
118
#include <stdlib.h>
119
#include <reent.h>
120
#include <_syslist.h>
121
 
122
int
123
_DEFUN (_init_signal_r, (ptr),
124
        struct _reent *ptr)
125
{
126
  int i;
127
 
128
  if (ptr->_sig_func == NULL)
129
    {
130
      ptr->_sig_func = (_sig_func_ptr *)_malloc_r (ptr, sizeof (_sig_func_ptr) * NSIG);
131
      if (ptr->_sig_func == NULL)
132
        return -1;
133
 
134
      for (i = 0; i < NSIG; i++)
135
        ptr->_sig_func[i] = SIG_DFL;
136
    }
137
 
138
  return 0;
139
}
140
 
141
_sig_func_ptr
142
_DEFUN (_signal_r, (ptr, sig, func),
143
        struct _reent *ptr _AND
144
        int sig _AND
145
        _sig_func_ptr func)
146
{
147
  _sig_func_ptr old_func, *temp;
148
 
149
  if (sig < 0 || sig >= NSIG)
150
    {
151
      ptr->_errno = EINVAL;
152
      return SIG_ERR;
153
    }
154
 
155
  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
156
    return SIG_ERR;
157
 
158
  old_func = ptr->_sig_func[sig];
159
  ptr->_sig_func[sig] = func;
160
 
161
  return old_func;
162
}
163
 
164
int
165
_raise_r (ptr, sig)
166
     struct _reent *ptr;
167
     int sig;
168
{
169
  _sig_func_ptr func;
170
  int result = 0;
171
 
172
  if (sig < 0 || sig >= NSIG)
173
    {
174
      ptr->_errno = EINVAL;
175
      return -1;
176
    }
177
 
178
  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
179
    return -1;
180
 
181
  switch ((_POINTER_INT) ptr->_sig_func[sig])
182
    {
183
    case SIG_DFL:
184
      return _kill_r (ptr, _getpid_r (ptr), sig);
185
 
186
    case SIG_IGN:
187
      break;
188
 
189
    case SIG_ERR:
190
      ptr->_errno = EINVAL;
191
      result = 1;
192
      break;
193
 
194
    default:
195
      func = ptr->_sig_func[sig];
196
      ptr->_sig_func[sig] = SIG_DFL;
197
      func (sig);
198
      break;
199
    }
200
 
201
  return result;
202
}
203
 
204
int
205
__sigtramp_r (ptr, sig)
206
     struct _reent *ptr;
207
     int sig;
208
{
209
  _sig_func_ptr func;
210
 
211
  if (sig < 0 || sig >= NSIG)
212
    {
213
      return -1;
214
    }
215
 
216
  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
217
    return -1;
218
 
219
  switch ((_POINTER_INT) ptr->_sig_func[sig])
220
    {
221
    case SIG_DFL:
222
      return 1;
223
 
224
    case SIG_ERR:
225
      return 2;
226
 
227
    case SIG_IGN:
228
      return 3;
229
 
230
    default:
231
      func = ptr->_sig_func[sig];
232
      ptr->_sig_func[sig] = SIG_DFL;
233
      func (sig);
234
      return 0;
235
    }
236
}
237
 
238
#ifndef _REENT_ONLY
239
 
240
int
241
raise (sig)
242
     int sig;
243
{
244
  return _raise_r (_REENT, sig);
245
}
246
 
247
_sig_func_ptr
248
_DEFUN (signal, (sig, func),
249
        int sig _AND
250
        _sig_func_ptr func)
251
{
252
  return _signal_r (_REENT, sig, func);
253
}
254
 
255
int
256
_init_signal ()
257
{
258
  return _init_signal_r (_REENT);
259
}
260
 
261
int
262
__sigtramp (int sig)
263
{
264
  return __sigtramp_r (_REENT, sig);
265
}
266
 
267
#endif
268
 
269
#endif /* !SIGNAL_PROVIDED */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.