2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
18 #include <sys/types.h>
20 #include <sys/socket.h>
25 /* wire protocol magic must match */
26 #define UDEV_CTRL_MAGIC 0xdead1dea
28 enum udev_ctrl_msg_type {
30 UDEV_CTRL_SET_LOG_LEVEL,
31 UDEV_CTRL_STOP_EXEC_QUEUE,
32 UDEV_CTRL_START_EXEC_QUEUE,
35 UDEV_CTRL_SET_CHILDREN_MAX,
40 struct udev_ctrl_msg_wire {
43 enum udev_ctrl_msg_type type;
50 struct udev_ctrl_msg {
52 struct udev_ctrl_connection *conn;
53 struct udev_ctrl_msg_wire ctrl_msg_wire;
60 struct sockaddr_un saddr;
67 struct udev_ctrl_connection {
69 struct udev_ctrl *uctrl;
73 struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd) {
74 struct udev_ctrl *uctrl;
77 uctrl = new0(struct udev_ctrl, 1);
84 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
85 if (uctrl->sock < 0) {
86 log_error("error getting socket: %m");
87 udev_ctrl_unref(uctrl);
94 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
96 uctrl->saddr.sun_family = AF_LOCAL;
97 strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
98 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
102 struct udev_ctrl *udev_ctrl_new(struct udev *udev) {
103 return udev_ctrl_new_from_fd(udev, -1);
106 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
110 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
111 if (err < 0 && errno == EADDRINUSE) {
112 unlink(uctrl->saddr.sun_path);
113 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
118 log_error("bind failed: %m");
122 err = listen(uctrl->sock, 0);
125 log_error("listen failed: %m");
130 uctrl->cleanup_socket = true;
135 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl) {
139 static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) {
146 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) {
150 if (uctrl->refcount > 0)
152 if (uctrl->sock >= 0)
158 int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
161 if (uctrl->cleanup_socket)
162 unlink(uctrl->saddr.sun_path);
166 int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
172 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
173 struct udev_ctrl_connection *conn;
174 struct ucred ucred = {};
178 conn = new(struct udev_ctrl_connection, 1);
184 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
185 if (conn->sock < 0) {
187 log_error("unable to receive ctrl connection: %m");
191 /* check peer credential of connection */
192 r = getpeercred(conn->sock, &ucred);
194 log_error("unable to receive credentials of ctrl connection: %s", strerror(-r));
198 log_error("sender uid=%i, message ignored", ucred.uid);
202 /* enable receiving of the sender credentials in the messages */
203 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
204 udev_ctrl_ref(uctrl);
213 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) {
220 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) {
224 if (conn->refcount > 0)
228 udev_ctrl_unref(conn->uctrl);
233 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
234 struct udev_ctrl_msg_wire ctrl_msg_wire;
237 memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
238 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
239 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
240 ctrl_msg_wire.type = type;
243 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
245 ctrl_msg_wire.intval = intval;
247 if (!uctrl->connected) {
248 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
252 uctrl->connected = true;
254 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
259 /* wait for peer message handling or disconnect */
261 struct pollfd pfd[1];
264 pfd[0].fd = uctrl->sock;
265 pfd[0].events = POLLIN;
266 r = poll(pfd, 1, timeout * MSEC_PER_SEC);
274 if (r > 0 && pfd[0].revents & POLLERR) {
287 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
288 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
291 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
292 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
295 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
296 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
299 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
300 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
303 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
304 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
307 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
308 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
311 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
312 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
315 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
316 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
319 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
320 struct udev_ctrl_msg *uctrl_msg;
322 struct cmsghdr *cmsg;
324 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
325 struct msghdr smsg = {
328 .msg_control = cred_msg,
329 .msg_controllen = sizeof(cred_msg),
333 uctrl_msg = new0(struct udev_ctrl_msg, 1);
334 if (uctrl_msg == NULL)
336 uctrl_msg->refcount = 1;
337 uctrl_msg->conn = conn;
338 udev_ctrl_connection_ref(conn);
340 /* wait for the incoming message */
342 struct pollfd pfd[1];
345 pfd[0].fd = conn->sock;
346 pfd[0].events = POLLIN;
348 r = poll(pfd, 1, 10000);
354 log_error("timeout waiting for ctrl message");
357 if (!(pfd[0].revents & POLLIN)) {
358 log_error("ctrl connection error: %m");
366 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
367 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
369 size = recvmsg(conn->sock, &smsg, 0);
371 log_error("unable to receive ctrl message: %m");
374 cmsg = CMSG_FIRSTHDR(&smsg);
375 cred = (struct ucred *) CMSG_DATA(cmsg);
377 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
378 log_error("no sender credentials received, message ignored");
382 if (cred->uid != 0) {
383 log_error("sender uid=%i, message ignored", cred->uid);
387 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
388 log_error("message magic 0x%08x doesn't match, ignore it", uctrl_msg->ctrl_msg_wire.magic);
394 udev_ctrl_msg_unref(uctrl_msg);
398 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) {
399 if (ctrl_msg == NULL)
401 ctrl_msg->refcount--;
402 if (ctrl_msg->refcount > 0)
404 udev_ctrl_connection_unref(ctrl_msg->conn);
409 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
410 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
411 return ctrl_msg->ctrl_msg_wire.intval;
415 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
416 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
421 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
422 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
427 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
428 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
433 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
434 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
435 return ctrl_msg->ctrl_msg_wire.buf;
439 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
440 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
441 return ctrl_msg->ctrl_msg_wire.intval;
445 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
446 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
451 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
452 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)