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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [signal/] [signal.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
/*
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;
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
 
171
  if (sig < 0 || sig >= NSIG)
172
    {
173
      ptr->_errno = EINVAL;
174
      return -1;
175
    }
176
 
177
  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
178
    return -1;
179
 
180
  func = ptr->_sig_func[sig];
181
  if (func == SIG_DFL)
182
    return _kill_r (ptr, _getpid_r (ptr), sig);
183
  else if (func == SIG_IGN)
184
    return 0;
185
  else if (func == SIG_ERR)
186
    {
187
      ptr->_errno = EINVAL;
188
      return 1;
189
    }
190
  else
191
    {
192
      ptr->_sig_func[sig] = SIG_DFL;
193
      func (sig);
194
      return 0;
195
    }
196
}
197
 
198
int
199
__sigtramp_r (ptr, sig)
200
     struct _reent *ptr;
201
     int sig;
202
{
203
  _sig_func_ptr func;
204
 
205
  if (sig < 0 || sig >= NSIG)
206
    {
207
      return -1;
208
    }
209
 
210
  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
211
    return -1;
212
 
213
  func = ptr->_sig_func[sig];
214
  if (func == SIG_DFL)
215
    return 1;
216
  else if (func == SIG_ERR)
217
    return 2;
218
  else if (func == SIG_IGN)
219
    return 3;
220
  else
221
    {
222
      ptr->_sig_func[sig] = SIG_DFL;
223
      func (sig);
224
      return 0;
225
    }
226
}
227
 
228
#ifndef _REENT_ONLY
229
 
230
int
231
raise (sig)
232
     int sig;
233
{
234
  return _raise_r (_REENT, sig);
235
}
236
 
237
_sig_func_ptr
238
_DEFUN (signal, (sig, func),
239
        int sig _AND
240
        _sig_func_ptr func)
241
{
242
  return _signal_r (_REENT, sig, func);
243
}
244
 
245
int
246
_init_signal ()
247
{
248
  return _init_signal_r (_REENT);
249
}
250
 
251
int
252
__sigtramp (int sig)
253
{
254
  return __sigtramp_r (_REENT, sig);
255
}
256
 
257
#endif
258
 
259
#endif /* !SIGNAL_PROVIDED */

powered by: WebSVN 2.1.0

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