1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// signal3.cxx
|
4 |
|
|
//
|
5 |
|
|
// POSIX signal test 3
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): nickg
|
43 |
|
|
// Contributors: nickg
|
44 |
|
|
// Date: 2003-01-30
|
45 |
|
|
// Description: Tests POSIX signal functionality.
|
46 |
|
|
//
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
//==========================================================================
|
49 |
|
|
|
50 |
|
|
#include <cyg/infra/testcase.h>
|
51 |
|
|
#include <pkgconf/posix.h>
|
52 |
|
|
|
53 |
|
|
#if !defined(CYGPKG_POSIX_SIGNALS)
|
54 |
|
|
#define NA_MSG "POSIX signals not enabled"
|
55 |
|
|
#elif !defined(CYGPKG_POSIX_PTHREAD)
|
56 |
|
|
#define NA_MSG "POSIX threads not enabled"
|
57 |
|
|
#endif
|
58 |
|
|
|
59 |
|
|
#ifdef NA_MSG
|
60 |
|
|
void
|
61 |
|
|
cyg_start(void)
|
62 |
|
|
{
|
63 |
|
|
CYG_TEST_INIT();
|
64 |
|
|
CYG_TEST_NA(NA_MSG);
|
65 |
|
|
}
|
66 |
|
|
#else
|
67 |
|
|
|
68 |
|
|
#include <signal.h>
|
69 |
|
|
#include <pthread.h>
|
70 |
|
|
#include <stdio.h>
|
71 |
|
|
#include <errno.h>
|
72 |
|
|
#include <cyg/infra/diag.h>
|
73 |
|
|
|
74 |
|
|
volatile int sigusr1_called = 0;
|
75 |
|
|
|
76 |
|
|
//--------------------------------------------------------------------------
|
77 |
|
|
// Signal handler functions
|
78 |
|
|
|
79 |
|
|
static void sigusr1( int signo )
|
80 |
|
|
{
|
81 |
|
|
CYG_TEST_INFO( "sigusr1() handler called" );
|
82 |
|
|
CYG_TEST_CHECK( signo == SIGUSR1, "Signal not SIGUSR1");
|
83 |
|
|
|
84 |
|
|
sigusr1_called++;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
//--------------------------------------------------------------------------
|
88 |
|
|
|
89 |
|
|
int main (int argc, char **argv)
|
90 |
|
|
{
|
91 |
|
|
int ret_val;
|
92 |
|
|
sigset_t set;
|
93 |
|
|
int sig;
|
94 |
|
|
struct itimerspec timerValue; // Timeout value on eCos
|
95 |
|
|
timer_t timer1; // Timer
|
96 |
|
|
struct sigevent sev;
|
97 |
|
|
|
98 |
|
|
CYG_TEST_INIT();
|
99 |
|
|
|
100 |
|
|
{
|
101 |
|
|
struct sigaction sa;
|
102 |
|
|
|
103 |
|
|
sa.sa_handler = sigusr1;
|
104 |
|
|
sigfillset( &sa.sa_mask );
|
105 |
|
|
sa.sa_flags = 0;
|
106 |
|
|
|
107 |
|
|
ret_val = sigaction( SIGUSR1, &sa, NULL );
|
108 |
|
|
|
109 |
|
|
CYG_TEST_CHECK( ret_val == 0 , "sigaction returned error");
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
// unblock all the signals
|
113 |
|
|
sigfillset (&set);
|
114 |
|
|
pthread_sigmask (SIG_UNBLOCK, &set, (sigset_t*)NULL);
|
115 |
|
|
|
116 |
|
|
//--------------------------------------------------------------------
|
117 |
|
|
// <start of timer initialization section>
|
118 |
|
|
//--------------------------------------------------------------------
|
119 |
|
|
|
120 |
|
|
// Notification type --- Deliver the signal
|
121 |
|
|
sev.sigev_notify = SIGEV_SIGNAL;
|
122 |
|
|
sev.sigev_signo = SIGUSR1;
|
123 |
|
|
sev.sigev_value.sival_int = 0xABCDEF01;
|
124 |
|
|
|
125 |
|
|
// Timer values --- 1 Second
|
126 |
|
|
timerValue.it_value.tv_sec = 1;
|
127 |
|
|
timerValue.it_value.tv_nsec = 0;
|
128 |
|
|
timerValue.it_interval.tv_sec = 1;
|
129 |
|
|
timerValue.it_interval.tv_nsec = 0;
|
130 |
|
|
|
131 |
|
|
ret_val = timer_create (CLOCK_REALTIME, &sev, &timer1);
|
132 |
|
|
|
133 |
|
|
CYG_TEST_CHECK( ret_val==0, "Error in creating the timer");
|
134 |
|
|
|
135 |
|
|
ret_val = timer_settime (timer1, 0, &timerValue, NULL );
|
136 |
|
|
CYG_TEST_CHECK( ret_val==0,"Error in setting the time");
|
137 |
|
|
|
138 |
|
|
//--------------------------------------------------------------------
|
139 |
|
|
// <end of timer initialization section>
|
140 |
|
|
//--------------------------------------------------------------------
|
141 |
|
|
|
142 |
|
|
CYG_TEST_INFO ("Timer initialisation is completed..");
|
143 |
|
|
|
144 |
|
|
CYG_TEST_INFO ("Calling pause()");
|
145 |
|
|
ret_val = pause();
|
146 |
|
|
CYG_TEST_CHECK( ret_val==-1, "pause() did not return -1");
|
147 |
|
|
CYG_TEST_CHECK( EINTR==errno, "errno set to EINTR");
|
148 |
|
|
CYG_TEST_CHECK( sigusr1_called==1, "Siguser1 handler not called");
|
149 |
|
|
|
150 |
|
|
// Block all the signals
|
151 |
|
|
sigfillset (&set);
|
152 |
|
|
pthread_sigmask (SIG_BLOCK, &set, (sigset_t*)NULL);
|
153 |
|
|
|
154 |
|
|
CYG_TEST_INFO ("Calling sigwait()");
|
155 |
|
|
// Wait for any signal to arrive
|
156 |
|
|
sigfillset (&set);
|
157 |
|
|
ret_val = sigwait (&set, &sig);
|
158 |
|
|
|
159 |
|
|
CYG_TEST_CHECK( ret_val==0, "sigwait returned error");
|
160 |
|
|
CYG_TEST_CHECK( sig==SIGUSR1, "sigwait returned wrong signo!");
|
161 |
|
|
CYG_TEST_CHECK( sigusr1_called==1, "Siguser1 handler called!");
|
162 |
|
|
|
163 |
|
|
CYG_TEST_INFO ("Program terminating");
|
164 |
|
|
|
165 |
|
|
CYG_TEST_PASS_FINISH( "signal3" );
|
166 |
|
|
return 0;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
#endif
|
171 |
|
|
|
172 |
|
|
//--------------------------------------------------------------------------
|
173 |
|
|
// end of signal3.c
|