2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@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)
75 struct udev_ctrl *uctrl;
77 uctrl = calloc(1, sizeof(struct udev_ctrl));
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\n");
87 udev_ctrl_unref(uctrl);
95 uctrl->saddr.sun_family = AF_LOCAL;
96 util_strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
97 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
101 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)
111 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
112 if (err < 0 && errno == EADDRINUSE) {
113 unlink(uctrl->saddr.sun_path);
114 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
119 log_error("bind failed: %m\n");
123 err = listen(uctrl->sock, 0);
126 log_error("listen failed: %m\n");
131 uctrl->cleanup_socket = true;
136 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
141 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
149 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
154 if (uctrl->refcount > 0)
156 if (uctrl->sock >= 0)
162 int udev_ctrl_cleanup(struct udev_ctrl *uctrl)
166 if (uctrl->cleanup_socket)
167 unlink(uctrl->saddr.sun_path);
171 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
178 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
180 struct udev_ctrl_connection *conn;
185 conn = calloc(1, sizeof(struct udev_ctrl_connection));
191 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
192 if (conn->sock < 0) {
194 log_error("unable to receive ctrl connection: %m\n");
198 /* check peer credential of connection */
199 slen = sizeof(ucred);
200 if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
201 log_error("unable to receive credentials of ctrl connection: %m\n");
205 log_error("sender uid=%i, message ignored\n", ucred.uid);
209 /* enable receiving of the sender credentials in the messages */
210 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
211 udev_ctrl_ref(uctrl);
220 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
228 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
233 if (conn->refcount > 0)
237 udev_ctrl_unref(conn->uctrl);
242 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
244 struct udev_ctrl_msg_wire ctrl_msg_wire;
247 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
248 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
249 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
250 ctrl_msg_wire.type = type;
253 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
255 ctrl_msg_wire.intval = intval;
257 if (!uctrl->connected) {
258 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
262 uctrl->connected = true;
264 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
269 /* wait for peer message handling or disconnect */
271 struct pollfd pfd[1];
274 pfd[0].fd = uctrl->sock;
275 pfd[0].events = POLLIN;
276 r = poll(pfd, 1, timeout * 1000);
284 if (r > 0 && pfd[0].revents & POLLERR) {
297 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
299 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
302 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
304 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
307 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
309 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
312 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout)
314 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
317 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
319 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
322 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
324 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
327 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
329 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
332 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
334 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
337 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
339 struct udev_ctrl_msg *uctrl_msg;
342 struct cmsghdr *cmsg;
345 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
347 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
348 if (uctrl_msg == NULL)
350 uctrl_msg->refcount = 1;
351 uctrl_msg->conn = conn;
352 udev_ctrl_connection_ref(conn);
354 /* wait for the incoming message */
356 struct pollfd pfd[1];
359 pfd[0].fd = conn->sock;
360 pfd[0].events = POLLIN;
362 r = poll(pfd, 1, 10000);
368 log_error("timeout waiting for ctrl message\n");
371 if (!(pfd[0].revents & POLLIN)) {
372 log_error("ctrl connection error: %m\n");
380 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
381 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
382 memset(&smsg, 0x00, sizeof(struct msghdr));
385 smsg.msg_control = cred_msg;
386 smsg.msg_controllen = sizeof(cred_msg);
387 size = recvmsg(conn->sock, &smsg, 0);
389 log_error("unable to receive ctrl message: %m\n");
392 cmsg = CMSG_FIRSTHDR(&smsg);
393 cred = (struct ucred *) CMSG_DATA(cmsg);
395 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
396 log_error("no sender credentials received, message ignored\n");
400 if (cred->uid != 0) {
401 log_error("sender uid=%i, message ignored\n", cred->uid);
405 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
406 log_error("message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
412 udev_ctrl_msg_unref(uctrl_msg);
416 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
418 if (ctrl_msg == NULL)
420 ctrl_msg->refcount++;
424 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
426 if (ctrl_msg == NULL)
428 ctrl_msg->refcount--;
429 if (ctrl_msg->refcount > 0)
431 udev_ctrl_connection_unref(ctrl_msg->conn);
436 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
438 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
439 return ctrl_msg->ctrl_msg_wire.intval;
443 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
445 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
450 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
452 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
457 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg)
459 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
464 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
466 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
467 return ctrl_msg->ctrl_msg_wire.buf;
471 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
473 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
474 return ctrl_msg->ctrl_msg_wire.intval;
478 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
480 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
485 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
487 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)