source: rtems/cpukit/posix/src/killinfo.c @ 1c2d178

Last change on this file since 1c2d178 was 1c2d178, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 25, 2018 at 7:15:26 PM

score: Remove Objects_Information::maximum

This information is already present in Objects_Information::maximum_id.
Add and use _Objects_Get_maximum_index().

Update #3621.

  • Property mode set to 100644
File size: 10.4 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
200    /*
201     *  This can occur when no one is interested and an API is not configured.
202     */
203    if ( !_Objects_Information_table[ the_api ] )
204      continue;
205
206    the_info = _Objects_Information_table[ the_api ][ 1 ];
207    if ( !the_info )
208      continue;
209
210    maximum = _Objects_Get_maximum_index( the_info );
211    object_table = the_info->local_table;
212
213    for ( index = 0 ; index < maximum ; ++index ) {
214      the_thread = (Thread_Control *) object_table[ index ];
215
216      if ( !the_thread )
217        continue;
218
219      #if defined(DEBUG_SIGNAL_PROCESSING)
220        printk("\n 0x%08x/0x%08x %d/%d 0x%08x 1",
221          the_thread->Object.id,
222          ((interested) ? interested->Object.id : 0),
223          _Thread_Get_priority( the_thread ), interested_priority,
224          the_thread->current_state
225        );
226      #endif
227
228      /*
229       *  If this thread is of lower priority than the interested thread,
230       *  go on to the next thread.
231       */
232      if ( _Thread_Get_priority( the_thread ) > interested_priority )
233        continue;
234      DEBUG_STEP("2");
235
236      /*
237       *  If this thread is not interested, then go on to the next thread.
238       */
239      api = the_thread->API_Extensions[ THREAD_API_POSIX ];
240
241      #if defined(RTEMS_DEBUG)
242        if ( !api )
243          continue;
244      #endif
245
246      if ( !_POSIX_signals_Is_interested( api, mask ) )
247        continue;
248      DEBUG_STEP("3");
249
250      /*
251       *  Now we know the thread under consideration is interested.
252       *  If the thread under consideration is of higher priority, then
253       *  it becomes the interested thread.
254       *
255       *  NOTE: We initialized interested_priority to PRIORITY_MAXIMUM + 1
256       *        so we never have to worry about deferencing a NULL
257       *        interested thread.
258       */
259      if ( _Thread_Get_priority( the_thread ) < interested_priority ) {
260        interested   = the_thread;
261        interested_priority = _Thread_Get_priority( the_thread );
262        continue;
263      }
264      DEBUG_STEP("4");
265
266      /*
267       *  Now the thread and the interested thread have the same priority.
268       *  We have to sort through the combinations of blocked/not blocked
269       *  and blocking interruptibutable by signal.
270       *
271       *  If the interested thread is ready, don't think about changing.
272       */
273
274      if ( interested && !_States_Is_ready( interested->current_state ) ) {
275        /* preferred ready over blocked */
276        DEBUG_STEP("5");
277        if ( _States_Is_ready( the_thread->current_state ) ) {
278          interested          = the_thread;
279          interested_priority = _Thread_Get_priority( the_thread );
280          continue;
281        }
282
283        DEBUG_STEP("6");
284        /* prefer blocked/interruptible over blocked/not interruptible */
285        if ( !_States_Is_interruptible_by_signal(interested->current_state) ) {
286          DEBUG_STEP("7");
287          if ( _States_Is_interruptible_by_signal(the_thread->current_state) ) {
288            DEBUG_STEP("8");
289            interested          = the_thread;
290            interested_priority = _Thread_Get_priority( the_thread );
291            continue;
292          }
293        }
294      }
295    }
296  }
297
298  if ( interested ) {
299    the_thread = interested;
300    goto process_it;
301  }
302
303  /*
304   *  OK so no threads were interested right now.  It will be left on the
305   *  global pending until a thread receives it.  The global set of threads
306   *  can change interest in this signal in one of the following ways:
307   *
308   *    + a thread is created with the signal unblocked,
309   *    + pthread_sigmask() unblocks the signal,
310   *    + sigprocmask() unblocks the signal, OR
311   *    + sigaction() which changes the handler to SIG_IGN.
312   */
313  the_thread = NULL;
314  goto post_process_signal;
315
316  /*
317   *  We found a thread which was interested, so now we mark that this
318   *  thread needs to do the post context switch extension so it can
319   *  evaluate the signals pending.
320   */
321process_it:
322
323  /*
324   *  Returns true if the signal was synchronously given to a thread
325   *  blocked waiting for the signal.
326   */
327  if ( _POSIX_signals_Unblock_thread( the_thread, sig, siginfo ) ) {
328    _Thread_Dispatch_enable( cpu_self );
329    return 0;
330  }
331
332post_process_signal:
333
334  /*
335   *  We may have woken up a thread but we definitely need to post the
336   *  signal to the process wide information set.
337   */
338  _POSIX_signals_Set_process_signals( mask );
339
340  _Thread_queue_Context_initialize( &queue_context );
341  _POSIX_signals_Acquire( &queue_context );
342
343  if ( _POSIX_signals_Vectors[ sig ].sa_flags == SA_SIGINFO ) {
344
345    psiginfo = (POSIX_signals_Siginfo_node *)
346      _Chain_Get_unprotected( &_POSIX_signals_Inactive_siginfo );
347    if ( !psiginfo ) {
348      _POSIX_signals_Release( &queue_context );
349      _Thread_Dispatch_enable( cpu_self );
350      rtems_set_errno_and_return_minus_one( EAGAIN );
351    }
352
353    psiginfo->Info = *siginfo;
354
355    _Chain_Append_unprotected(
356      &_POSIX_signals_Siginfo[ sig ],
357      &psiginfo->Node
358    );
359  }
360
361  _POSIX_signals_Release( &queue_context );
362  DEBUG_STEP("\n");
363  _Thread_Dispatch_enable( cpu_self );
364  return 0;
365}
Note: See TracBrowser for help on using the repository browser.