source: rtems/cpukit/posix/src/psignal.c @ 3c895256

5
Last change on this file since 3c895256 was 3c895256, checked in by Sebastian Huber <sebastian.huber@…>, on 04/05/16 at 04:44:07

posix: Make _POSIX_signals_Check_signal() static

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief POSIX Signals Manager Initialization
5 *  @ingroup POSIX_SIGNALS
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2008.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <errno.h>
22#include <signal.h>
23#include <string.h>
24#include <stdlib.h>
25
26#include <rtems/score/isrlevel.h>
27#include <rtems/score/threadimpl.h>
28#include <rtems/score/threadqimpl.h>
29#include <rtems/score/watchdogimpl.h>
30#include <rtems/score/wkspace.h>
31#include <rtems/posix/threadsup.h>
32#include <rtems/posix/psignalimpl.h>
33#include <rtems/posix/pthreadimpl.h>
34#include <rtems/config.h>
35#include <rtems/seterr.h>
36#include <rtems/sysinit.h>
37
38/*
39 *  Ensure we have the same number of vectors and default vector entries
40 */
41RTEMS_STATIC_ASSERT(
42  sizeof( _POSIX_signals_Vectors ) == sizeof( _POSIX_signals_Default_vectors ),
43  posix_signals_vectors
44);
45
46/*** PROCESS WIDE STUFF ****/
47
48sigset_t  _POSIX_signals_Pending;
49
50void _POSIX_signals_Abnormal_termination_handler(
51  int signo RTEMS_UNUSED )
52{
53  exit( 1 );
54}
55
56#define SIG_ARRAY_MAX  (SIGRTMAX + 1)
57const struct sigaction _POSIX_signals_Default_vectors[ SIG_ARRAY_MAX ] = {
58  /* NO SIGNAL 0 */  SIGACTION_IGNORE,
59  /* SIGHUP    1 */  SIGACTION_TERMINATE,
60  /* SIGINT    2 */  SIGACTION_TERMINATE,
61  /* SIGQUIT   3 */  SIGACTION_TERMINATE,
62  /* SIGILL    4 */  SIGACTION_TERMINATE,
63  /* SIGTRAP   5 */  SIGACTION_TERMINATE,
64  /* SIGIOT    6 */  SIGACTION_TERMINATE,
65  /* SIGABRT   6     SIGACTION_TERMINATE, -- alias for SIGIOT */
66  /* SIGEMT    7 */  SIGACTION_TERMINATE,
67  /* SIGFPE    8 */  SIGACTION_TERMINATE,
68  /* SIGKILL   9 */  SIGACTION_TERMINATE,
69  /* SIGBUS   10 */  SIGACTION_TERMINATE,
70  /* SIGSEGV  11 */  SIGACTION_TERMINATE,
71  /* SIGSYS   12 */  SIGACTION_TERMINATE,
72  /* SIGPIPE  13 */  SIGACTION_TERMINATE,
73  /* SIGALRM  14 */  SIGACTION_TERMINATE,
74  /* SIGTERM  15 */  SIGACTION_TERMINATE,
75  /* SIGURG   16 */  SIGACTION_TERMINATE,
76  /* SIGSTOP  17 */  SIGACTION_TERMINATE,
77  /* SIGTSTP  18 */  SIGACTION_TERMINATE,
78  /* SIGCONT  19 */  SIGACTION_TERMINATE,
79  /* SIGCHLD  20 */  SIGACTION_TERMINATE,
80  /* SIGTTIN  21 */  SIGACTION_TERMINATE,
81  /* SIGTTOU  22 */  SIGACTION_TERMINATE,
82  /* SIGIO    23 */  SIGACTION_TERMINATE,
83  /* SIGWINCH 24 */  SIGACTION_TERMINATE,
84  /* SIGUSR1  25 */  SIGACTION_TERMINATE,
85  /* SIGUSR2  26 */  SIGACTION_TERMINATE,
86  /* SIGRT    27 */  SIGACTION_TERMINATE,
87  /* SIGRT    28 */  SIGACTION_TERMINATE,
88  /* SIGRT    29 */  SIGACTION_TERMINATE,
89  /* SIGRT    30 */  SIGACTION_TERMINATE,
90  /* SIGRTMAX 31 */  SIGACTION_TERMINATE
91};
92
93struct sigaction _POSIX_signals_Vectors[ SIG_ARRAY_MAX ];
94
95Thread_queue_Control _POSIX_signals_Wait_queue =
96  THREAD_QUEUE_INITIALIZER( "POSIX Signals" );
97
98Chain_Control _POSIX_signals_Inactive_siginfo;
99Chain_Control _POSIX_signals_Siginfo[ SIG_ARRAY_MAX ];
100
101/*
102 *  XXX - move these
103 */
104
105#define _States_Is_interruptible_signal( _states ) \
106  ( ((_states) & \
107    (STATES_WAITING_FOR_SIGNAL|STATES_INTERRUPTIBLE_BY_SIGNAL)) == \
108      (STATES_WAITING_FOR_SIGNAL|STATES_INTERRUPTIBLE_BY_SIGNAL))
109
110static void _POSIX_signals_Check_signal(
111  POSIX_API_Control  *api,
112  int                 signo,
113  bool                is_global
114)
115{
116  siginfo_t                   siginfo_struct;
117  sigset_t                    saved_signals_unblocked;
118  Thread_Wait_information     stored_thread_wait_information;
119  Thread_Control             *executing;
120
121  if ( ! _POSIX_signals_Clear_signals( api, signo, &siginfo_struct,
122                                       is_global, true, true ) )
123    return;
124
125  /*
126   *  Since we made a union of these, only one test is necessary but this is
127   *  safer.
128   */
129  #if defined(RTEMS_DEBUG)
130    assert( _POSIX_signals_Vectors[ signo ].sa_handler ||
131            _POSIX_signals_Vectors[ signo ].sa_sigaction );
132  #endif
133
134  /*
135   *  Just to prevent sending a signal which is currently being ignored.
136   */
137  if ( _POSIX_signals_Vectors[ signo ].sa_handler == SIG_IGN )
138    return;
139
140  /*
141   *  Block the signals requested in sa_mask
142   */
143  saved_signals_unblocked = api->signals_unblocked;
144  api->signals_unblocked &= ~_POSIX_signals_Vectors[ signo ].sa_mask;
145
146  executing = _Thread_Get_executing();
147
148  /*
149   *  We have to save the blocking information of the current wait queue
150   *  because the signal handler may subsequently go on and put the thread
151   *  on a wait queue, for its own purposes.
152   */
153  memcpy( &stored_thread_wait_information, &executing->Wait,
154          sizeof( stored_thread_wait_information ));
155
156  /*
157   *  Here, the signal handler function executes
158   */
159  switch ( _POSIX_signals_Vectors[ signo ].sa_flags ) {
160    case SA_SIGINFO:
161      (*_POSIX_signals_Vectors[ signo ].sa_sigaction)(
162        signo,
163        &siginfo_struct,
164        NULL        /* context is undefined per 1003.1b-1993, p. 66 */
165      );
166      break;
167    default:
168      (*_POSIX_signals_Vectors[ signo ].sa_handler)( signo );
169      break;
170  }
171
172  /*
173   *  Restore the blocking information
174   */
175  memcpy( &executing->Wait, &stored_thread_wait_information,
176          sizeof( executing->Wait ));
177
178  /*
179   *  Restore the previous set of unblocked signals
180   */
181  api->signals_unblocked = saved_signals_unblocked;
182}
183
184void _POSIX_signals_Action_handler(
185  Thread_Control  *executing,
186  Thread_Action   *action,
187  Per_CPU_Control *cpu,
188  ISR_Level        level
189)
190{
191  POSIX_API_Control  *api;
192  int                 signo;
193  ISR_lock_Context    lock_context;
194  int                 hold_errno;
195
196  (void) action;
197  _Thread_Action_release_and_ISR_enable( cpu, level );
198
199  api = executing->API_Extensions[ THREAD_API_POSIX ];
200
201  /*
202   *  We need to ensure that if the signal handler executes a call
203   *  which overwrites the unblocking status, we restore it.
204   */
205  hold_errno = executing->Wait.return_code;
206
207  /*
208   * api may be NULL in case of a thread close in progress
209   */
210  if ( !api )
211    return;
212
213  /*
214   *  If we invoke any user code, there is the possibility that
215   *  a new signal has been posted that we should process so we
216   *  restart the loop if a signal handler was invoked.
217   *
218   *  The first thing done is to check there are any signals to be
219   *  processed at all.  No point in doing this loop otherwise.
220   */
221  while (1) {
222    _POSIX_signals_Acquire( &lock_context );
223      if ( !(api->signals_unblocked &
224            (api->signals_pending | _POSIX_signals_Pending)) ) {
225       _POSIX_signals_Release( &lock_context );
226       break;
227     }
228    _POSIX_signals_Release( &lock_context );
229
230    for ( signo = SIGRTMIN ; signo <= SIGRTMAX ; signo++ ) {
231      _POSIX_signals_Check_signal( api, signo, false );
232      _POSIX_signals_Check_signal( api, signo, true );
233    }
234    /* Unfortunately - nothing like __SIGFIRSTNOTRT in newlib signal .h */
235
236    for ( signo = SIGHUP ; signo <= __SIGLASTNOTRT ; signo++ ) {
237      _POSIX_signals_Check_signal( api, signo, false );
238      _POSIX_signals_Check_signal( api, signo, true );
239    }
240  }
241
242  executing->Wait.return_code = hold_errno;
243}
244
245static void _POSIX_signals_Manager_Initialization(void)
246{
247  uint32_t   signo;
248  uint32_t   maximum_queued_signals;
249
250  maximum_queued_signals = Configuration_POSIX_API.maximum_queued_signals;
251
252  memcpy(
253    _POSIX_signals_Vectors,
254    _POSIX_signals_Default_vectors,
255    sizeof( _POSIX_signals_Vectors )
256  );
257
258  /*
259   *  Initialize the set of pending signals for the entire process
260   */
261  sigemptyset( &_POSIX_signals_Pending );
262
263  /* XXX status codes */
264
265  /*
266   *  Allocate the siginfo pools.
267   */
268  for ( signo=1 ; signo<= SIGRTMAX ; signo++ )
269    _Chain_Initialize_empty( &_POSIX_signals_Siginfo[ signo ] );
270
271  if ( maximum_queued_signals ) {
272    _Chain_Initialize(
273      &_POSIX_signals_Inactive_siginfo,
274      _Workspace_Allocate_or_fatal_error(
275        maximum_queued_signals * sizeof( POSIX_signals_Siginfo_node )
276      ),
277      maximum_queued_signals,
278      sizeof( POSIX_signals_Siginfo_node )
279    );
280  } else {
281    _Chain_Initialize_empty( &_POSIX_signals_Inactive_siginfo );
282  }
283}
284
285RTEMS_SYSINIT_ITEM(
286  _POSIX_signals_Manager_Initialization,
287  RTEMS_SYSINIT_POSIX_SIGNALS,
288  RTEMS_SYSINIT_ORDER_MIDDLE
289);
Note: See TracBrowser for help on using the repository browser.