libuev 2.4.1
signal.c
Go to the documentation of this file.
1/* libuEv - Micro event loop library
2 *
3 * Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
4 * Copyright (c) 2013-2024 Joachim Wiberg <troglobit()gmail!com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include <errno.h>
26#include <signal.h>
27#include <sys/signalfd.h>
28#include <unistd.h> /* close(), read() */
29
30#include "uev.h"
31
40
41
52int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo)
53{
54 sigset_t mask;
55 int fd;
56
57 if (!w || !ctx) {
58 errno = EINVAL;
59 return -1;
60 }
61 w->fd = -1;
62
63 sigemptyset(&mask);
64 fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
65 if (fd < 0)
66 return -1;
67
68 if (_uev_watcher_init(ctx, w, UEV_SIGNAL_TYPE, cb, arg, fd, UEV_READ))
69 goto exit;
70
71 if (uev_signal_set(w, signo)) {
72 _uev_watcher_stop(w);
73 exit:
74 close(fd);
75 return -1;
76 }
77
78 return 0;
79}
80
88int uev_signal_set(uev_t *w, int signo)
89{
90 sigset_t mask;
91
92 /* Every watcher must be registered to a context */
93 if (!w || !w->ctx) {
94 errno = EINVAL;
95 return -1;
96 }
97
98 /* Remember for callbacks and start/stop */
99 w->signo = signo;
100
101 /* Handle stopped signal watchers */
102 if (w->fd < 0) {
103 if (uev_signal_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, signo))
104 return -1;
105 }
106
107 sigemptyset(&mask);
108 sigaddset(&mask, signo);
109
110 /* Block signals so that they aren't handled
111 according to their default dispositions */
112 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
113 return -1;
114
115 if (signalfd(w->fd, &mask, SFD_NONBLOCK) < 0)
116 return -1;
117
118 return _uev_watcher_start(w);
119}
120
121
129{
130 if (!w) {
131 errno = EINVAL;
132 return -1;
133 }
134
135 if (-1 != w->fd)
137
138 return uev_signal_set(w, w->signo);
139}
140
148{
149 if (!_uev_watcher_active(w))
150 return 0;
151
152 if (_uev_watcher_stop(w))
153 return -1;
154
155 close(w->fd);
156 w->fd = -1;
157
158 return 0;
159}
160
int uev_signal_stop(uev_t *w)
Definition signal.c:147
int uev_signal_start(uev_t *w)
Definition signal.c:128
int uev_signal_set(uev_t *w, int signo)
Definition signal.c:88
int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo)
Definition signal.c:52
int signo
Definition uev.h:78
int fd
Definition uev.h:79
uev_ctx_t * ctx
Definition uev.h:80
struct uev_ctx uev_ctx_t
Definition uev.h:70
#define UEV_READ
Definition uev.h:46
void uev_cb_t(uev_t *w, void *arg, int events)
Definition uev.h:97
struct uev uev_t