forked from martinpitt/umockdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuevent_sender.c
302 lines (261 loc) · 8.7 KB
/
uevent_sender.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2012 Canonical Ltd.
* Author: Martin Pitt <[email protected]>
*
* umockdev 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.
*
* umockdev 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 program; If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <glob.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/un.h>
#include <unistd.h>
#include <libudev.h>
#include "utils.h"
#include "uevent_sender.h"
#define UEVENT_BUFSIZE 16384
struct _uevent_sender {
char *rootpath;
char socket_glob[PATH_MAX];
struct udev *udev;
};
uevent_sender *
uevent_sender_open(const char *rootpath)
{
uevent_sender *s;
assert(rootpath != NULL);
s = calloc(1, sizeof(uevent_sender));
if (!s) {
perror("uevent_sender_open: cannot allocate struct");
abort();
}
s->rootpath = strdupx(rootpath);
s->udev = udev_new();
snprintf(s->socket_glob, sizeof(s->socket_glob), "%s/event[0-9]*", rootpath);
return s;
}
void
uevent_sender_close(uevent_sender * sender)
{
udev_unref(sender->udev);
free(sender->rootpath);
free(sender);
}
static void
sendmsg_one(struct iovec *iov, size_t iov_len, const char *path)
{
struct sockaddr_un event_addr;
int fd;
int ret;
/* create uevent socket address */
strncpy(event_addr.sun_path, path, sizeof(event_addr.sun_path) - 1);
event_addr.sun_family = AF_UNIX;
/* create uevent socket */
fd = socket(AF_UNIX, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (fd < 0) {
perror("sendmsg_one: cannot create socket");
abort();
}
ret = connect(fd, (struct sockaddr *)&event_addr, sizeof(event_addr));
if (ret < 0) {
if (errno == ECONNREFUSED) {
/* client side closed its monitor underneath us, so clean up and ignore */
unlink(event_addr.sun_path);
close(fd);
return;
}
perror("sendmsg_one: cannot connect to client's event socket");
abort();
}
const struct msghdr msg = { .msg_name = &event_addr, .msg_iov = iov, .msg_iovlen = iov_len };
ssize_t count = sendmsg(fd, &msg, 0);
if (count < 0) {
perror("uevent_sender sendmsg_one: sendmsg failed");
abort();
}
/* printf("passed %zi bytes to event socket %s\n", count, path); */
close(fd);
}
static void
sendmsg_all(uevent_sender * sender, struct iovec *iov, size_t iov_len)
{
glob_t gl;
int res;
/* find current listeners */
res = glob(sender->socket_glob, GLOB_NOSORT, NULL, &gl);
if (res == 0) {
size_t i;
for (i = 0; i < gl.gl_pathc; ++i)
sendmsg_one(iov, iov_len, gl.gl_pathv[i]);
} else {
/* ensure that we only fail due to that, not due to bad globs */
if (res != GLOB_NOMATCH) {
fprintf(stderr, "ERROR: sendmsg_all: %s glob failed with %i\n",
sender->socket_glob, res);
abort();
}
}
globfree(&gl);
}
#define UDEV_MONITOR_MAGIC 0xfeedcafe
struct udev_monitor_netlink_header {
/* "libudev" prefix to distinguish libudev and kernel messages */
char prefix[8];
/*
* magic to protect against daemon <-> library message format mismatch
* used in the kernel from socket filter rules; needs to be stored in network order
*/
unsigned int magic;
/* total length of header structure known to the sender */
unsigned int header_size;
/* properties string buffer */
unsigned int properties_off;
unsigned int properties_len;
/*
* hashes of primary device properties strings, to let libudev subscribers
* use in-kernel socket filters; values need to be stored in network order
*/
unsigned int filter_subsystem_hash;
unsigned int filter_devtype_hash;
unsigned int filter_tag_bloom_hi;
unsigned int filter_tag_bloom_lo;
};
/* taken from systemd/src/basic/MurmurHash2.c */
static uint32_t
string_hash32(const char *str)
{
/*
* 'm' and 'r' are mixing constants generated offline.
* They're not really 'magic', they just happen to work well.
*/
const uint32_t m = 0x5bd1e995;
const int r = 24;
int len = strlen(str);
/* initialize the hash to a 'random' value */
uint32_t h = len;
/* mix 4 bytes at a time into the hash */
const unsigned char *data = (const unsigned char *)str;
while (len >= 4) {
uint32_t k = 0;
memcpy(&k, data, sizeof(uint32_t));
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
/* handle the last few bytes of the input array */
switch (len) {
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
/* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
static size_t
append_property(char *array, size_t size, size_t offset, const char *name, const char *value)
{
int r;
assert(offset < size);
r = snprintf(array + offset, size - offset, "%s%s", name, value);
if (r < 0) {
fprintf(stderr, "ERROR: snprintf failed");
abort();
}
if (r + offset >= size) {
fprintf(stderr, "ERROR: uevent_sender_send: Property buffer overflow\n");
abort();
}
/* this is already true as snprintf always writes the NUL, but -fanalyzer complains about use-of-uninitialized-value */
array[offset + r] = '\0';
/* include the NUL terminator in the string length, as we need to keep it as a separator between keys */
return r + 1;
}
/* this mirrors the code from systemd/src/libsystemd/sd-device/device-monitor.c,
* device_monitor_send_device() */
void
uevent_sender_send(uevent_sender * sender, const char *devpath, const char *action, const char *properties)
{
char buffer[UEVENT_BUFSIZE];
size_t buffer_len = 0;
struct iovec iov[2];
const char *subsystem;
const char *devtype;
char seqnumstr[20];
struct udev_device *device;
struct udev_monitor_netlink_header nlh;
static unsigned long long seqnum = 1;
device = udev_device_new_from_syspath(sender->udev, devpath);
if (device == NULL) {
fprintf(stderr, "ERROR: uevent_sender_send: No such device %s\n", devpath);
return;
}
subsystem = udev_device_get_subsystem(device);
assert(subsystem != NULL);
devtype = udev_device_get_devtype(device);
/* build NUL-terminated property array */
buffer_len += append_property(buffer, sizeof buffer, buffer_len, "ACTION=", action);
buffer_len += append_property(buffer, sizeof buffer, buffer_len, "DEVPATH=", udev_device_get_devpath(device));
buffer_len += append_property(buffer, sizeof buffer, buffer_len, "SUBSYSTEM=", subsystem);
snprintf(seqnumstr, sizeof(seqnumstr), "%llu", seqnum++);
buffer_len += append_property(buffer, sizeof buffer, buffer_len, "SEQNUM=", seqnumstr);
/* append udevd (userland) properties, replace \n with \0 */
/* FIXME: more sensible API */
size_t properties_len = properties ? strlen(properties) : 0;
if (properties_len > 0) {
size_t prop_ofs = buffer_len;
buffer_len += append_property(buffer, sizeof buffer, buffer_len, properties, "");
for (size_t i = prop_ofs; i < buffer_len - 1; ++i)
if (buffer[i] == '\n')
buffer[i] = '\0';
/* avoid empty property at the end from final line break */
if (properties[strlen(properties) - 1] == '\n')
--buffer_len;
}
/* add versioned header */
memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
memcpy(nlh.prefix, "libudev", 8);
nlh.magic = htonl(UDEV_MONITOR_MAGIC);
nlh.header_size = sizeof(struct udev_monitor_netlink_header);
nlh.filter_subsystem_hash = htonl(string_hash32(subsystem));
if (devtype != NULL)
nlh.filter_devtype_hash = htonl(string_hash32(devtype));
iov[0].iov_base = &nlh;
iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
udev_device_unref(device);
/* note, not setting nlh.filter_tag_bloom_{hi,lo} for now; if required, copy
* from libudev */
/* add properties list */
nlh.properties_off = iov[0].iov_len;
nlh.properties_len = buffer_len;
iov[1].iov_base = buffer;
iov[1].iov_len = buffer_len;
/* send message */
sendmsg_all(sender, iov, 2);
}