• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.59
/src/udev/udev-ctrl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <poll.h>
5
#include <stddef.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <sys/un.h>
9
#include <unistd.h>
10

11
#include "sd-event.h"
12

13
#include "alloc-util.h"
14
#include "errno-util.h"
15
#include "fd-util.h"
16
#include "format-util.h"
17
#include "iovec-util.h"
18
#include "log.h"
19
#include "socket-util.h"
20
#include "strxcpyx.h"
21
#include "udev-ctrl.h"
22

23
/* wire protocol magic must match */
24
#define UDEV_CTRL_MAGIC                                0xdead1dea
25

26
typedef struct UdevCtrlMessageWire {
27
        char version[16];
28
        unsigned magic;
29
        UdevCtrlMessageType type;
30
        UdevCtrlMessageValue value;
31
} UdevCtrlMessageWire;
32

33
struct UdevCtrl {
34
        unsigned n_ref;
35
        int sock;
36
        int sock_connect;
37
        union sockaddr_union saddr;
38
        socklen_t addrlen;
39
        bool bound;
40
        bool connected;
41
        sd_event *event;
42
        sd_event_source *event_source;
43
        sd_event_source *event_source_connect;
44
        udev_ctrl_handler_t callback;
45
        void *userdata;
46
};
47

48
int udev_ctrl_new_from_fd(UdevCtrl **ret, int fd) {
55✔
49
        _cleanup_close_ int sock = -EBADF;
55✔
50
        UdevCtrl *uctrl;
55✔
51

52
        assert(ret);
55✔
53

54
        if (fd < 0) {
55✔
55
                sock = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
×
56
                if (sock < 0)
×
57
                        return log_error_errno(errno, "Failed to create socket: %m");
×
58
        }
59

60
        uctrl = new(UdevCtrl, 1);
55✔
61
        if (!uctrl)
55✔
62
                return -ENOMEM;
63

64
        *uctrl = (UdevCtrl) {
110✔
65
                .n_ref = 1,
66
                .sock = fd >= 0 ? fd : TAKE_FD(sock),
55✔
67
                .sock_connect = -EBADF,
68
                .bound = fd >= 0,
55✔
69
        };
70

71
        uctrl->saddr.un = (struct sockaddr_un) {
55✔
72
                .sun_family = AF_UNIX,
73
                .sun_path = "/run/udev/control",
74
        };
75

76
        uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
55✔
77

78
        *ret = TAKE_PTR(uctrl);
55✔
79
        return 0;
55✔
80
}
81

82
int udev_ctrl_enable_receiving(UdevCtrl *uctrl) {
110✔
83
        assert(uctrl);
110✔
84

85
        if (uctrl->bound)
110✔
86
                return 0;
87

88
        (void) sockaddr_un_unlink(&uctrl->saddr.un);
×
89
        if (bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
×
90
                return log_error_errno(errno, "Failed to bind udev control socket: %m");
×
91

92
        if (listen(uctrl->sock, 0) < 0)
×
93
                return log_error_errno(errno, "Failed to listen udev control socket: %m");
×
94

95
        uctrl->bound = true;
×
96
        return 0;
×
97
}
98

99
static void udev_ctrl_disconnect(UdevCtrl *uctrl) {
55✔
100
        if (!uctrl)
55✔
101
                return;
102

103
        uctrl->event_source_connect = sd_event_source_unref(uctrl->event_source_connect);
55✔
104
        uctrl->sock_connect = safe_close(uctrl->sock_connect);
55✔
105
}
106

107
static UdevCtrl *udev_ctrl_free(UdevCtrl *uctrl) {
55✔
108
        assert(uctrl);
55✔
109

110
        udev_ctrl_disconnect(uctrl);
55✔
111

112
        sd_event_source_unref(uctrl->event_source);
55✔
113
        safe_close(uctrl->sock);
55✔
114

115
        sd_event_unref(uctrl->event);
55✔
116
        return mfree(uctrl);
55✔
117
}
118

119
DEFINE_TRIVIAL_REF_UNREF_FUNC(UdevCtrl, udev_ctrl, udev_ctrl_free);
110✔
120

121
int udev_ctrl_attach_event(UdevCtrl *uctrl, sd_event *event) {
55✔
122
        int r;
55✔
123

124
        assert_return(uctrl, -EINVAL);
55✔
125
        assert_return(!uctrl->event, -EBUSY);
55✔
126

127
        if (event)
55✔
128
                uctrl->event = sd_event_ref(event);
55✔
129
        else {
130
                r = sd_event_default(&uctrl->event);
×
131
                if (r < 0)
×
132
                        return r;
×
133
        }
134

135
        return 0;
136
}
137

138
sd_event_source *udev_ctrl_get_event_source(UdevCtrl *uctrl) {
55✔
139
        assert(uctrl);
55✔
140

141
        return uctrl->event_source;
55✔
142
}
143

144
static void udev_ctrl_disconnect_and_listen_again(UdevCtrl *uctrl) {
×
145
        udev_ctrl_disconnect(uctrl);
×
146
        udev_ctrl_unref(uctrl);
×
147
        (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_ON);
×
148
        /* We don't return NULL here because uctrl is not freed */
149
}
×
150

151
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(UdevCtrl*, udev_ctrl_disconnect_and_listen_again, NULL);
×
152

153
static int udev_ctrl_connection_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
×
154
        _cleanup_(udev_ctrl_disconnect_and_listen_againp) UdevCtrl *uctrl = NULL;
×
155
        UdevCtrlMessageWire msg_wire;
×
156
        struct iovec iov = IOVEC_MAKE(&msg_wire, sizeof(UdevCtrlMessageWire));
×
157
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
×
158
        struct msghdr smsg = {
×
159
                .msg_iov = &iov,
160
                .msg_iovlen = 1,
161
                .msg_control = &control,
162
                .msg_controllen = sizeof(control),
163
        };
164
        struct ucred *cred;
×
165
        ssize_t size;
×
166

167
        assert(userdata);
×
168

169
        /* When UDEV_CTRL_EXIT is received, manager unref udev_ctrl object.
170
         * To avoid the object freed, let's increment the refcount. */
171
        uctrl = udev_ctrl_ref(userdata);
×
172

173
        size = recvmsg_safe(fd, &smsg, 0);
×
174
        if (ERRNO_IS_NEG_TRANSIENT(size))
×
175
                return 0;
176
        if (size == -ECHRNG) {
×
177
                log_warning_errno(size, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
178
                return 0;
×
179
        }
180
        if (size == -EXFULL) {
×
181
                log_warning_errno(size, "Got message with truncated payload data, ignoring.");
×
182
                return 0;
×
183
        }
184
        if (size < 0)
×
185
                return log_error_errno(size, "Failed to receive ctrl message: %m");
×
186

187
        cmsg_close_all(&smsg);
×
188

189
        if (size != sizeof(msg_wire)) {
×
190
                log_warning("Received message with invalid length, ignoring");
×
191
                return 0;
×
192
        }
193

194
        cred = CMSG_FIND_DATA(&smsg, SOL_SOCKET, SCM_CREDENTIALS, struct ucred);
×
195
        if (!cred) {
×
196
                log_warning("No sender credentials received, ignoring message");
×
197
                return 0;
×
198
        }
199

200
        if (cred->uid != 0) {
×
201
                log_warning("Invalid sender uid "UID_FMT", ignoring message", cred->uid);
×
202
                return 0;
×
203
        }
204

205
        if (msg_wire.magic != UDEV_CTRL_MAGIC) {
×
206
                log_warning("Message magic 0x%08x doesn't match, ignoring message", msg_wire.magic);
×
207
                return 0;
×
208
        }
209

210
        if (msg_wire.type == _UDEV_CTRL_END_MESSAGES)
×
211
                return 0;
212

213
        if (uctrl->callback)
×
214
                (void) uctrl->callback(uctrl, msg_wire.type, &msg_wire.value, uctrl->userdata);
×
215

216
        /* Do not disconnect and wait for next message. */
217
        uctrl = udev_ctrl_unref(uctrl);
×
218
        return 0;
219
}
220

221
static int udev_ctrl_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
×
222
        UdevCtrl *uctrl = ASSERT_PTR(userdata);
×
223
        _cleanup_close_ int sock = -EBADF;
×
224
        struct ucred ucred;
×
225
        int r;
×
226

227
        sock = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
×
228
        if (sock < 0) {
×
229
                if (ERRNO_IS_ACCEPT_AGAIN(errno))
×
230
                        return 0;
231

232
                return log_error_errno(errno, "Failed to accept ctrl connection: %m");
×
233
        }
234

235
        /* check peer credential of connection */
236
        r = getpeercred(sock, &ucred);
×
237
        if (r < 0) {
×
238
                log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
×
239
                return 0;
×
240
        }
241

242
        if (ucred.uid > 0) {
×
243
                log_error("Invalid sender uid "UID_FMT", closing connection", ucred.uid);
×
244
                return 0;
×
245
        }
246

247
        /* enable receiving of the sender credentials in the messages */
248
        r = setsockopt_int(sock, SOL_SOCKET, SO_PASSCRED, true);
×
249
        if (r < 0)
×
250
                log_warning_errno(r, "Failed to set SO_PASSCRED, ignoring: %m");
×
251

252
        r = sd_event_add_io(uctrl->event, &uctrl->event_source_connect, sock, EPOLLIN, udev_ctrl_connection_event_handler, uctrl);
×
253
        if (r < 0) {
×
254
                log_error_errno(r, "Failed to create event source for udev control connection: %m");
×
255
                return 0;
×
256
        }
257

258
        (void) sd_event_source_set_description(uctrl->event_source_connect, "udev-ctrl-connection");
×
259

260
        /* Do not accept multiple connection. */
261
        (void) sd_event_source_set_enabled(uctrl->event_source, SD_EVENT_OFF);
×
262

263
        uctrl->sock_connect = TAKE_FD(sock);
×
264
        return 0;
×
265
}
266

267
int udev_ctrl_start(UdevCtrl *uctrl, udev_ctrl_handler_t callback, void *userdata) {
55✔
268
        int r;
55✔
269

270
        assert(uctrl);
55✔
271

272
        if (!uctrl->event) {
55✔
273
                r = udev_ctrl_attach_event(uctrl, NULL);
×
274
                if (r < 0)
×
275
                        return r;
276
        }
277

278
        r = udev_ctrl_enable_receiving(uctrl);
55✔
279
        if (r < 0)
55✔
280
                return r;
281

282
        uctrl->callback = callback;
55✔
283
        uctrl->userdata = userdata;
55✔
284

285
        r = sd_event_add_io(uctrl->event, &uctrl->event_source, uctrl->sock, EPOLLIN, udev_ctrl_event_handler, uctrl);
55✔
286
        if (r < 0)
55✔
287
                return r;
288

289
        (void) sd_event_source_set_description(uctrl->event_source, "udev-ctrl");
55✔
290

291
        return 0;
55✔
292
}
293

294
int udev_ctrl_send(UdevCtrl *uctrl, UdevCtrlMessageType type, const void *data) {
×
295
        UdevCtrlMessageWire ctrl_msg_wire = {
×
296
                .version = "udev-" STRINGIFY(PROJECT_VERSION),
297
                .magic = UDEV_CTRL_MAGIC,
298
                .type = type,
299
        };
300

301
        if (type == UDEV_CTRL_SET_ENV) {
×
302
                assert(data);
×
303
                strscpy(ctrl_msg_wire.value.buf, sizeof(ctrl_msg_wire.value.buf), data);
×
304
        } else if (IN_SET(type, UDEV_CTRL_SET_LOG_LEVEL, UDEV_CTRL_SET_CHILDREN_MAX))
×
305
                ctrl_msg_wire.value.intval = PTR_TO_INT(data);
×
306

307
        if (!uctrl->connected) {
×
308
                if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0)
×
309
                        return -errno;
×
310
                uctrl->connected = true;
×
311
        }
312

313
        if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0)
×
314
                return -errno;
×
315

316
        return 0;
317
}
318

319
int udev_ctrl_wait(UdevCtrl *uctrl, usec_t timeout) {
×
320
        _cleanup_(sd_event_source_disable_unrefp) sd_event_source *source_io = NULL, *source_timeout = NULL;
×
321
        int r;
×
322

323
        assert(uctrl);
×
324

325
        if (uctrl->sock < 0)
×
326
                return 0;
327
        if (!uctrl->connected)
×
328
                return 0;
329

330
        r = udev_ctrl_send(uctrl, _UDEV_CTRL_END_MESSAGES, NULL);
×
331
        if (r < 0)
×
332
                return r;
333

334
        if (timeout == 0)
×
335
                return 0;
336

337
        if (!uctrl->event) {
×
338
                r = udev_ctrl_attach_event(uctrl, NULL);
×
339
                if (r < 0)
×
340
                        return r;
341
        }
342

343
        r = sd_event_add_io(uctrl->event, &source_io, uctrl->sock, EPOLLIN, NULL, INT_TO_PTR(0));
×
344
        if (r < 0)
×
345
                return r;
346

347
        (void) sd_event_source_set_description(source_io, "udev-ctrl-wait-io");
×
348

349
        if (timeout != USEC_INFINITY) {
×
350
                r = sd_event_add_time_relative(
×
351
                                uctrl->event, &source_timeout, CLOCK_BOOTTIME,
352
                                timeout,
353
                                0, NULL, INT_TO_PTR(-ETIMEDOUT));
354
                if (r < 0)
×
355
                        return r;
356

357
                (void) sd_event_source_set_description(source_timeout, "udev-ctrl-wait-timeout");
×
358
        }
359

360
        return sd_event_loop(uctrl->event);
×
361
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc