1 |
/* PEAK Library - Test file |
2 |
* |
3 |
* Copyright (c) 2004 |
4 |
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
19 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
* |
29 |
*/ |
30 |
#define RCSID "$Id: t_signal1.c,v 1.3 2004/01/09 20:19:48 mbuna Exp $" |
31 |
|
32 |
/* t_signal1.c - Signal handling test program. |
33 |
* |
34 |
* Purpose |
35 |
* ======= |
36 |
* Configure some signals and see if they are handled properly on a one |
37 |
* thread task. Use a peak timer to send signals. |
38 |
* |
39 |
* Returns |
40 |
* ======= |
41 |
* 0 -> test ok |
42 |
* 1 -> peak signal handler creation failed (error) |
43 |
* 2 -> bad signum parameter (error) |
44 |
* 3 -> bad context pointer management (error) |
45 |
* 4 -> one or more signals lost (warning) |
46 |
* 5 -> other timer error (error) |
47 |
* |
48 |
*/ |
49 |
|
50 |
#define T_ERR_SIGNAL_CREATE_FAILED 1 |
51 |
#define T_ERR_SIGNAL_BAD_SIGNUM 2 |
52 |
#define T_ERR_SIGNAL_BAD_CONTEXT 3 |
53 |
#define T_WARN_SIGNAL_LOST 4 |
54 |
#define T_ERR_TIMER_ERROR 5 |
55 |
|
56 |
#ifdef HAVE_CONFIG_H |
57 |
#include "config.h" |
58 |
#endif |
59 |
|
60 |
#include <peak/peak.h> |
61 |
#include <stdlib.h> |
62 |
#include <sys/types.h> |
63 |
#include <signal.h> |
64 |
|
65 |
#define SIGNAL1_CONTEXT ((void*)0xc86642a9) |
66 |
|
67 |
static int cnt, sent; |
68 |
|
69 |
static void |
70 |
signal_event_cb(peak_signal i, int value, void *context) |
71 |
{ |
72 |
cnt++; |
73 |
|
74 |
if (cnt > 30) |
75 |
peak_task_break(peak_task_self()); |
76 |
|
77 |
if (value != SIGHUP) |
78 |
exit(T_ERR_SIGNAL_BAD_SIGNUM); |
79 |
if (context != SIGNAL1_CONTEXT) |
80 |
exit(T_ERR_SIGNAL_BAD_CONTEXT); |
81 |
} |
82 |
|
83 |
|
84 |
static void |
85 |
timer_callback(peak_timer ti, void *context) |
86 |
{ |
87 |
kill(getpid(), SIGHUP); |
88 |
sent++; |
89 |
#if 0 |
90 |
kill(getpid(), SIGHUP); |
91 |
sent++; |
92 |
#endif |
93 |
} |
94 |
|
95 |
int |
96 |
main(int argc, char *argv[]) |
97 |
{ |
98 |
peak_task task = peak_task_self(); |
99 |
peak_signal si; |
100 |
peak_timer ti; |
101 |
|
102 |
/* Force use of 1 thread max only. */ |
103 |
int val = 1; |
104 |
peak_task_set_info(task, PEAK_TASK_FLAVOR_NTHREADS, &val); |
105 |
|
106 |
/* Create signal object. */ |
107 |
si = peak_signal_create(SIGHUP, signal_event_cb, SIGNAL1_CONTEXT); |
108 |
if (si == 0) |
109 |
exit(T_ERR_SIGNAL_CREATE_FAILED); |
110 |
|
111 |
/* Schedule task. */ |
112 |
peak_signal_schedule(si, task); |
113 |
|
114 |
/* Create repeating timer. |
115 |
*/ |
116 |
ti = peak_timer_create(0.4, 0.04, timer_callback, 0); |
117 |
if (ti == 0) |
118 |
exit(T_ERR_TIMER_ERROR); |
119 |
|
120 |
/* Add timer to task. |
121 |
*/ |
122 |
peak_task_timer_add(task, ti); |
123 |
|
124 |
/* Run task. |
125 |
*/ |
126 |
peak_task_run(task); |
127 |
peak_release(si); |
128 |
peak_release(ti); |
129 |
|
130 |
if (cnt != sent) |
131 |
exit(T_WARN_SIGNAL_LOST); |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|