1 |
/* PEAK Library |
2 |
* |
3 |
* Copyright (c) 2003, 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: signal.c,v 1.2 2004/01/08 19:03:54 mbuna Exp $" |
31 |
|
32 |
#ifdef HAVE_CONFIG_H |
33 |
#include "config.h" |
34 |
#endif |
35 |
|
36 |
#include <peak/signal.h> |
37 |
|
38 |
#include <assert.h> |
39 |
#include <errno.h> |
40 |
#include <sys/types.h> |
41 |
#ifdef HAVE_SYS_UIO_H |
42 |
#include <sys/uio.h> |
43 |
#endif |
44 |
#include <stdarg.h> |
45 |
#include <stdlib.h> |
46 |
#include <stdio.h> |
47 |
#include <string.h> |
48 |
#ifdef HAVE_SIGNAL_H |
49 |
#include <signal.h> |
50 |
#endif |
51 |
#include <unistd.h> |
52 |
|
53 |
#include "engine_client.h" |
54 |
#include "internal.h" |
55 |
#include "task_private.h" |
56 |
|
57 |
static void __peak_signal_init(peak_signal i, va_list vp, void *ctcx); |
58 |
static void __peak_signal_finalize(peak_signal i); |
59 |
static void __peak_signal_event_process(peak_signal i, int ioevent, int data); |
60 |
|
61 |
struct __peak_signal |
62 |
{ |
63 |
PEAK_STRUCT_ENGINE_CLIENT_HEADER; |
64 |
peak_signal_event_callback _cb; |
65 |
void *_context; |
66 |
}; |
67 |
|
68 |
PEAK_CLASS_DECLARE(signal, engine_client); |
69 |
|
70 |
peak_signal |
71 |
peak_signal_create(int signum, peak_signal_event_callback cb, |
72 |
void *context) |
73 |
{ |
74 |
return PEAK_CLASS_CONSTRUCT3(signal, signum, cb, context); |
75 |
} |
76 |
|
77 |
static void |
78 |
__peak_signal_init(peak_signal i, va_list vp, void *ctcx) |
79 |
{ |
80 |
int signum = va_arg(vp, int); |
81 |
|
82 |
_peak_engine_client_configure((peak_engine_client)i, signum, CS_SIGNAL, |
83 |
(peak_engine_client_event_process_func)&__peak_signal_event_process); |
84 |
|
85 |
i->_cb = va_arg(vp, peak_signal_event_callback); |
86 |
i->_context = va_arg(vp, void*); |
87 |
} |
88 |
|
89 |
static void |
90 |
__peak_signal_finalize(peak_signal i) |
91 |
{ |
92 |
} |
93 |
|
94 |
/* Scheduling */ |
95 |
|
96 |
void |
97 |
peak_signal_schedule(peak_signal i, peak_task task) |
98 |
{ |
99 |
_peak_task_schedule_engine_client(task, (peak_engine_client)i); |
100 |
} |
101 |
|
102 |
void |
103 |
peak_signal_unschedule(peak_signal i, peak_task task) |
104 |
{ |
105 |
_peak_task_unschedule_engine_client(task, (peak_engine_client)i); |
106 |
} |
107 |
|
108 |
/* Convenience */ |
109 |
int |
110 |
peak_signal_ignore(int signum) |
111 |
{ |
112 |
struct sigaction act; |
113 |
act.sa_handler = SIG_IGN; |
114 |
act.sa_flags = 0; |
115 |
sigemptyset(&act.sa_mask); |
116 |
return sigaction(signum, &act, 0); |
117 |
} |
118 |
|
119 |
/* Processing */ |
120 |
|
121 |
static void |
122 |
__peak_signal_event_process(peak_signal i, int ioevent, int data) |
123 |
{ |
124 |
assert(ioevent == IOEVENT_SIGNAL); |
125 |
|
126 |
(*i->_cb)(i, data, i->_context); |
127 |
} |
128 |
|