-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainloop-notify.c
207 lines (161 loc) · 3.97 KB
/
mainloop-notify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "mainloop.h"
#include "mainloop-notify.h"
#include "timeout.h"
#include "util.h"
#include "io.h"
#define WATCHDOG_TRIGGER_FREQ 2
static int notify_fd = -1;
static unsigned int watchdog;
struct signal_data {
struct io *io;
mainloop_signal_func func;
void *user_data;
};
static struct signal_data *signal_data;
static bool watchdog_callback(void *user_data)
{
mainloop_sd_notify("WATCHDOG=1");
return true;
}
void mainloop_notify_init(void)
{
const char *sock;
struct sockaddr_un addr;
const char *watchdog_usec;
int msec;
sock = getenv("NOTIFY_SOCKET");
if (!sock)
return;
/* check for abstract socket or absolute path */
if (sock[0] != '@' && sock[0] != '/')
return;
notify_fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (notify_fd < 0)
return;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, sock, sizeof(addr.sun_path) - 1);
if (addr.sun_path[0] == '@')
addr.sun_path[0] = '\0';
if (connect(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(notify_fd);
notify_fd = -1;
return;
}
watchdog_usec = getenv("WATCHDOG_USEC");
if (!watchdog_usec)
return;
msec = atoi(watchdog_usec) / 1000;
if (msec < 0)
return;
watchdog = timeout_add(msec / WATCHDOG_TRIGGER_FREQ,
watchdog_callback, NULL, NULL);
}
void mainloop_notify_exit(void)
{
if (notify_fd > 0) {
close(notify_fd);
notify_fd = -1;
}
timeout_remove(watchdog);
}
int mainloop_sd_notify(const char *state)
{
int err;
if (notify_fd <= 0)
return -ENOTCONN;
err = send(notify_fd, state, strlen(state), MSG_NOSIGNAL);
if (err < 0)
return -errno;
return err;
}
static bool signal_read(struct io *io, void *user_data)
{
struct signal_data *data = user_data;
struct signalfd_siginfo si;
ssize_t result;
int fd;
fd = io_get_fd(io);
result = read(fd, &si, sizeof(si));
if (result != sizeof(si))
return false;
if (data && data->func)
data->func(si.ssi_signo, data->user_data);
return true;
}
static struct io *setup_signalfd(void *user_data)
{
struct io *io;
sigset_t mask;
int fd;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
return NULL;
fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
if (fd < 0)
return NULL;
io = io_new(fd);
io_set_close_on_destroy(io, true);
io_set_read_handler(io, signal_read, user_data, free);
return io;
}
int mainloop_run_with_signal(mainloop_signal_func func, void *user_data)
{
struct signal_data *data;
struct io *io;
int ret;
if (!func)
return -EINVAL;
data = new0(struct signal_data, 1);
data->func = func;
data->user_data = user_data;
io = setup_signalfd(data);
if (!io) {
free(data);
return -errno;
}
ret = mainloop_run();
io_destroy(io);
free(signal_data);
return ret;
}