source: rtems/cpukit/posix/src/sigtimedwait.c

Last change on this file was 380fb9f, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:32:05

cpukit/posix/src/[p-z]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 *  @file
5 *
6 *  @brief Wait for Queued Signals
7 *  @ingroup POSIXAPI
8 */
9
10/*
11 *  COPYRIGHT (c) 1989-2008.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <signal.h>
41
42#include <rtems/posix/pthreadimpl.h>
43#include <rtems/posix/psignalimpl.h>
44#include <rtems/posix/posixapi.h>
45#include <rtems/score/threadqimpl.h>
46#include <rtems/score/todimpl.h>
47#include <rtems/score/watchdogimpl.h>
48#include <rtems/score/isr.h>
49
50static int _POSIX_signals_Get_lowest(
51  sigset_t   set
52)
53{
54  int signo;
55
56  for ( signo = SIGRTMIN ; signo <= SIGRTMAX ; signo++ ) {
57    if ( set & signo_to_mask( signo ) ) {
58      goto found_it;
59    }
60  }
61
62  /*
63   *  We assume SIGHUP == 1 and is the first non-real-time signal.
64   */
65
66  #if (SIGHUP != 1)
67    #error "Assumption that SIGHUP==1 violated!!"
68  #endif
69  for ( signo = SIGHUP ; signo <= __SIGLASTNOTRT ; signo++ ) {
70    if ( set & signo_to_mask( signo ) ) {
71      goto found_it;
72    }
73  }
74
75  /*
76   *  This is structured this way to eliminate the need to have
77   *  a return 0.  This routine will NOT be called unless a signal
78   *  is pending in the set passed in.
79   */
80found_it:
81  return signo;
82}
83
84/**
85 *  3.3.8 Synchronously Accept a Signal, P1003.1b-1993, p. 76
86 */
87int sigtimedwait(
88  const sigset_t         *__restrict set,
89  siginfo_t              *__restrict info,
90  const struct timespec  *__restrict timeout
91)
92{
93  Thread_Control       *executing;
94  POSIX_API_Control    *api;
95  siginfo_t             signal_information;
96  siginfo_t            *the_info;
97  int                   signo;
98  Thread_queue_Context  queue_context;
99  int                   error;
100
101  /*
102   *  Error check parameters before disabling interrupts.
103   */
104  if ( !set )
105    rtems_set_errno_and_return_minus_one( EINVAL );
106
107  _Thread_queue_Context_initialize( &queue_context );
108
109  /*  NOTE: This is very specifically a RELATIVE not ABSOLUTE time
110   *        in the Open Group specification.
111   */
112
113  if ( timeout != NULL ) {
114    _Thread_queue_Context_set_enqueue_timeout_monotonic_timespec(
115      &queue_context,
116      timeout,
117      false
118    );
119  } else {
120    _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context );
121  }
122
123  /*
124   *  Initialize local variables.
125   */
126
127  the_info = ( info ) ? info : &signal_information;
128
129  executing = _Thread_Get_executing();
130  api = executing->API_Extensions[ THREAD_API_POSIX ];
131
132  /*
133   *  What if they are already pending?
134   */
135
136  /* API signals pending? */
137
138  _POSIX_signals_Acquire( &queue_context );
139  if ( *set & api->signals_pending ) {
140    /* XXX real info later */
141    the_info->si_signo = _POSIX_signals_Get_lowest( api->signals_pending );
142    _POSIX_signals_Clear_signals(
143      api,
144      the_info->si_signo,
145      the_info,
146      false,
147      false,
148      false
149    );
150    _POSIX_signals_Release( &queue_context );
151
152    the_info->si_code = SI_USER;
153    the_info->si_value.sival_int = 0;
154    return the_info->si_signo;
155  }
156
157  /* Process pending signals? */
158
159  if ( *set & _POSIX_signals_Pending ) {
160    signo = _POSIX_signals_Get_lowest( _POSIX_signals_Pending );
161    _POSIX_signals_Clear_signals( api, signo, the_info, true, false, false );
162    _POSIX_signals_Release( &queue_context );
163
164    the_info->si_signo = signo;
165    the_info->si_code = SI_USER;
166    the_info->si_value.sival_int = 0;
167    return signo;
168  }
169
170  the_info->si_signo = -1;
171
172  executing->Wait.option          = *set;
173  executing->Wait.return_argument = the_info;
174  _Thread_queue_Context_set_thread_state(
175    &queue_context,
176    STATES_WAITING_FOR_SIGNAL | STATES_INTERRUPTIBLE_BY_SIGNAL
177  );
178  _Thread_queue_Enqueue(
179    &_POSIX_signals_Wait_queue.Queue,
180    POSIX_SIGNALS_TQ_OPERATIONS,
181    executing,
182    &queue_context
183  );
184
185  /*
186   * When the thread is set free by a signal, it is need to eliminate
187   * the signal.
188   */
189
190  _POSIX_signals_Clear_signals(
191    api,
192    the_info->si_signo,
193    the_info,
194    false,
195    false,
196    true
197  );
198
199  /* Set errno only if return code is not EINTR or
200   * if EINTR was caused by a signal being caught, which
201   * was not in our set.
202   */
203
204  error = _POSIX_Get_error_after_wait( executing );
205
206  if (
207    error != EINTR
208     || ( *set & signo_to_mask( the_info->si_signo ) ) == 0
209  ) {
210    if ( error == ETIMEDOUT ) {
211      error = EAGAIN;
212    }
213
214    rtems_set_errno_and_return_minus_one( error );
215  }
216
217  return the_info->si_signo;
218}
Note: See TracBrowser for help on using the repository browser.