source: rtems/cpukit/posix/src/killinfo.c @ e0dcf294

5
Last change on this file since e0dcf294 was e0dcf294, checked in by Sebastian Huber <sebastian.huber@…>, on 04/05/19 at 07:58:07

Remove superfluous run-time check

The _Objects_Information_table[ the_api ] is never NULL for a valid API
index.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/**
2 * @file
3 *
4 * @brief Send a Signal to a Process
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  kill() support routine
10 *
11 *  COPYRIGHT (c) 1989-2009.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <pthread.h>
24#include <signal.h>
25#include <errno.h>
26
27#include <rtems/posix/pthreadimpl.h>
28#include <rtems/posix/psignalimpl.h>
29#include <rtems/score/isr.h>
30#include <rtems/score/schedulerimpl.h>
31#include <rtems/score/statesimpl.h>
32#include <rtems/seterr.h>
33
34/*
35 *  If you enable this, then you get printk() feedback on each path
36 *  and the input to the decision that lead to the decision.  Hopefully
37 *  this will help in debugging the algorithm that distributes process
38 *  signals to individual threads.
39 */
40
41/* #define DEBUG_SIGNAL_PROCESSING */
42#if defined(DEBUG_SIGNAL_PROCESSING)
43  #include <rtems/bspIo.h>
44  #define DEBUG_STEP(_x) printk(_x)
45#else
46  #define DEBUG_STEP(_x)
47#endif
48
49/*
50 *  3.3.2 Send a Signal to a Process, P1003.1b-1993, p. 68
51 *
52 *  NOTE: Behavior of kill() depends on _POSIX_SAVED_IDS.
53 */
54
55#define _POSIX_signals_Is_interested( _api, _mask ) \
56  ( (_api)->signals_unblocked & (_mask) )
57
58int _POSIX_signals_Send(
59  pid_t               pid,
60  int                 sig,
61  const union sigval *value
62)
63{
64  sigset_t                     mask;
65  POSIX_API_Control           *api;
66  uint32_t                     the_api;
67  uint32_t                     index;
68  uint32_t                     maximum;
69  Objects_Information         *the_info;
70  Objects_Control            **object_table;
71  Thread_Control              *the_thread;
72  Thread_Control              *interested;
73  Priority_Control             interested_priority;
74  Chain_Node                  *the_node;
75  siginfo_t                    siginfo_struct;
76  siginfo_t                   *siginfo;
77  POSIX_signals_Siginfo_node  *psiginfo;
78  Thread_queue_Heads          *heads;
79  Thread_queue_Context         queue_context;
80  Per_CPU_Control             *cpu_self;
81
82  /*
83   *  Only supported for the "calling process" (i.e. this node).
84   */
85  if ( pid != getpid() )
86    rtems_set_errno_and_return_minus_one( ESRCH );
87
88  /*
89   *  Validate the signal passed.
90   */
91  if ( !sig )
92    rtems_set_errno_and_return_minus_one( EINVAL );
93
94  if ( !is_valid_signo(sig) )
95    rtems_set_errno_and_return_minus_one( EINVAL );
96
97  /*
98   *  If the signal is being ignored, then we are out of here.
99   */
100  if ( _POSIX_signals_Vectors[ sig ].sa_handler == SIG_IGN )
101    return 0;
102
103  /*
104   *  P1003.1c/Draft 10, p. 33 says that certain signals should always
105   *  be directed to the executing thread such as those caused by hardware
106   *  faults.
107   */
108  if ( (sig == SIGFPE) || (sig == SIGILL) || (sig == SIGSEGV ) )
109      return pthread_kill( pthread_self(), sig );
110
111  mask = signo_to_mask( sig );
112
113  /*
114   *  Build up a siginfo structure
115   */
116  siginfo = &siginfo_struct;
117  siginfo->si_signo = sig;
118  siginfo->si_code = SI_USER;
119  if ( !value ) {
120    siginfo->si_value.sival_int = 0;
121  } else {
122    siginfo->si_value = *value;
123  }
124
125  /* FIXME: https://devel.rtems.org/ticket/2690 */
126  cpu_self = _Thread_Dispatch_disable();
127
128  /*
129   *  Is the currently executing thread interested?  If so then it will
130   *  get it an execute it as soon as the dispatcher executes.
131   */
132  the_thread = _Per_CPU_Get_executing( cpu_self );
133
134  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
135  if ( _POSIX_signals_Is_interested( api, mask ) ) {
136    goto process_it;
137  }
138
139  /*
140   *  Is an interested thread waiting for this signal (sigwait())?
141   *
142   *  There is no requirement on the order of threads pending on a sigwait().
143   */
144
145  /* XXX violation of visibility -- need to define thread queue support */
146
147  heads = _POSIX_signals_Wait_queue.Queue.heads;
148  if ( heads != NULL ) {
149    Chain_Control *the_chain = &heads->Heads.Fifo;
150
151    for ( the_node = _Chain_First( the_chain );
152          !_Chain_Is_tail( the_chain, the_node ) ;
153          the_node = the_node->next ) {
154      Scheduler_Node *scheduler_node;
155
156      scheduler_node = SCHEDULER_NODE_OF_WAIT_PRIORITY_NODE( the_node );
157      the_thread = _Scheduler_Node_get_owner( scheduler_node );
158      api = the_thread->API_Extensions[ THREAD_API_POSIX ];
159
160      #if defined(DEBUG_SIGNAL_PROCESSING)
161        printk( "Waiting Thread=%p option=0x%08x mask=0x%08x blocked=0x%08x\n",
162          the_thread, the_thread->Wait.option, mask, ~api->signals_unblocked);
163      #endif
164
165      /*
166       * Is this thread is actually blocked waiting for the signal?
167       */
168      if (the_thread->Wait.option & mask)
169        goto process_it;
170
171      /*
172       * Is this thread is blocked waiting for another signal but has
173       * not blocked this one?
174       */
175      if (api->signals_unblocked & mask)
176        goto process_it;
177    }
178  }
179
180  /*
181   *  Is any other thread interested?  The highest priority interested
182   *  thread is selected.  In the event of a tie, then the following
183   *  additional criteria is used:
184   *
185   *    + ready thread over blocked
186   *    + blocked on call interruptible by signal (can return EINTR)
187   *    + blocked on call not interruptible by signal
188   *
189   *  This looks at every thread in the system regardless of the creating API.
190   *
191   *  NOTES:
192   *
193   *    + rtems internal threads do not receive signals.
194   */
195  interested = NULL;
196  interested_priority = UINT64_MAX;
197
198  for (the_api = OBJECTS_CLASSIC_API; the_api <= OBJECTS_APIS_LAST; the_api++) {
199    _Assert( _Objects_Information_table[ the_api ] != NULL );
200    the_info = _Objects_Information_table[ the_api ][ 1 ];
201    if ( !the_info )
202      continue;
203
204    maximum = _Objects_Get_maximum_index( the_info );
205    object_table = the_info->local_table;
206
207    for ( index = 0 ; index < maximum ; ++index ) {
208      the_thread = (Thread_Control *) object_table[ index ];
209
210      if ( !the_thread )
211        continue;
212
213      #if defined(DEBUG_SIGNAL_PROCESSING)
214        printk("\n 0x%08x/0x%08x %d/%d 0x%08x 1",
215          the_thread->Object.id,
216          ((interested) ? interested->Object.id : 0),
217          _Thread_Get_priority( the_thread ), interested_priority,
218          the_thread->current_state
219        );
220      #endif
221
222      /*
223       *  If this thread is of lower priority than the interested thread,
224       *  go on to the next thread.
225       */
226      if ( _Thread_Get_priority( the_thread ) > interested_priority )
227        continue;
228      DEBUG_STEP("2");
229
230      /*
231       *  If this thread is not interested, then go on to the next thread.
232       */
233      api = the_thread->API_Extensions[ THREAD_API_POSIX ];
234
235      #if defined(RTEMS_DEBUG)
236        if ( !api )
237          continue;
238      #endif
239
240      if ( !_POSIX_signals_Is_interested( api, mask ) )
241        continue;
242      DEBUG_STEP("3");
243
244      /*
245       *  Now we know the thread under consideration is interested.
246       *  If the thread under consideration is of higher priority, then
247       *  it becomes the interested thread.
248       *
249       *  NOTE: We initialized interested_priority to PRIORITY_MAXIMUM + 1
250       *        so we never have to worry about deferencing a NULL
251       *        interested thread.
252       */
253      if ( _Thread_Get_priority( the_thread ) < interested_priority ) {
254        interested   = the_thread;
255        interested_priority = _Thread_Get_priority( the_thread );
256        continue;
257      }
258      DEBUG_STEP("4");
259
260      /*
261       *  Now the thread and the interested thread have the same priority.
262       *  We have to sort through the combinations of blocked/not blocked
263       *  and blocking interruptibutable by signal.
264       *
265       *  If the interested thread is ready, don't think about changing.
266       */
267
268      if ( interested && !_States_Is_ready( interested->current_state ) ) {
269        /* preferred ready over blocked */
270        DEBUG_STEP("5");
271        if ( _States_Is_ready( the_thread->current_state ) ) {
272          interested          = the_thread;
273          interested_priority = _Thread_Get_priority( the_thread );
274          continue;
275        }
276
277        DEBUG_STEP("6");
278        /* prefer blocked/interruptible over blocked/not interruptible */
279        if ( !_States_Is_interruptible_by_signal(interested->current_state) ) {
280          DEBUG_STEP("7");
281          if ( _States_Is_interruptible_by_signal(the_thread->current_state) ) {
282            DEBUG_STEP("8");
283            interested          = the_thread;
284            interested_priority = _Thread_Get_priority( the_thread );
285            continue;
286          }
287        }
288      }
289    }
290  }
291
292  if ( interested ) {
293    the_thread = interested;
294    goto process_it;
295  }
296
297  /*
298   *  OK so no threads were interested right now.  It will be left on the
299   *  global pending until a thread receives it.  The global set of threads
300   *  can change interest in this signal in one of the following ways:
301   *
302   *    + a thread is created with the signal unblocked,
303   *    + pthread_sigmask() unblocks the signal,
304   *    + sigprocmask() unblocks the signal, OR
305   *    + sigaction() which changes the handler to SIG_IGN.
306   */
307  the_thread = NULL;
308  goto post_process_signal;
309
310  /*
311   *  We found a thread which was interested, so now we mark that this
312   *  thread needs to do the post context switch extension so it can
313   *  evaluate the signals pending.
314   */
315process_it:
316
317  /*
318   *  Returns true if the signal was synchronously given to a thread
319   *  blocked waiting for the signal.
320   */
321  if ( _POSIX_signals_Unblock_thread( the_thread, sig, siginfo ) ) {
322    _Thread_Dispatch_enable( cpu_self );
323    return 0;
324  }
325
326post_process_signal:
327
328  /*
329   *  We may have woken up a thread but we definitely need to post the
330   *  signal to the process wide information set.
331   */
332  _POSIX_signals_Set_process_signals( mask );
333
334  _Thread_queue_Context_initialize( &queue_context );
335  _POSIX_signals_Acquire( &queue_context );
336
337  if ( _POSIX_signals_Vectors[ sig ].sa_flags == SA_SIGINFO ) {
338
339    psiginfo = (POSIX_signals_Siginfo_node *)
340      _Chain_Get_unprotected( &_POSIX_signals_Inactive_siginfo );
341    if ( !psiginfo ) {
342      _POSIX_signals_Release( &queue_context );
343      _Thread_Dispatch_enable( cpu_self );
344      rtems_set_errno_and_return_minus_one( EAGAIN );
345    }
346
347    psiginfo->Info = *siginfo;
348
349    _Chain_Append_unprotected(
350      &_POSIX_signals_Siginfo[ sig ],
351      &psiginfo->Node
352    );
353  }
354
355  _POSIX_signals_Release( &queue_context );
356  DEBUG_STEP("\n");
357  _Thread_Dispatch_enable( cpu_self );
358  return 0;
359}
Note: See TracBrowser for help on using the repository browser.