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)
75 struct udev_ctrl *uctrl;
78 uctrl = calloc(1, sizeof(struct udev_ctrl));
85 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
86 if (uctrl->sock < 0) {
87 log_error("error getting socket: %m\n");
88 udev_ctrl_unref(uctrl);
95 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
97 uctrl->saddr.sun_family = AF_LOCAL;
98 strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
99 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
103 struct udev_ctrl *udev_ctrl_new(struct udev *udev)
105 return udev_ctrl_new_from_fd(udev, -1);
108 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
113 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
114 if (err < 0 && errno == EADDRINUSE) {
115 unlink(uctrl->saddr.sun_path);
116 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
121 log_error("bind failed: %m\n");
125 err = listen(uctrl->sock, 0);
128 log_error("listen failed: %m\n");
133 uctrl->cleanup_socket = true;
138 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
143 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
151 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
156 if (uctrl->refcount > 0)
158 if (uctrl->sock >= 0)
164 int udev_ctrl_cleanup(struct udev_ctrl *uctrl)
168 if (uctrl->cleanup_socket)
169 unlink(uctrl->saddr.sun_path);
173 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
180 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
182 struct udev_ctrl_connection *conn;
187 conn = calloc(1, sizeof(struct udev_ctrl_connection));
193 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
194 if (conn->sock < 0) {
196 log_error("unable to receive ctrl connection: %m\n");
200 /* check peer credential of connection */
201 slen = sizeof(ucred);
202 if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
203 log_error("unable to receive credentials of ctrl connection: %m\n");
207 log_error("sender uid=%i, message ignored\n", ucred.uid);
211 /* enable receiving of the sender credentials in the messages */
212 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
213 udev_ctrl_ref(uctrl);
222 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
230 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
235 if (conn->refcount > 0)
239 udev_ctrl_unref(conn->uctrl);
244 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
246 struct udev_ctrl_msg_wire ctrl_msg_wire;
249 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
250 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
251 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
252 ctrl_msg_wire.type = type;
255 strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
257 ctrl_msg_wire.intval = intval;
259 if (!uctrl->connected) {
260 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
264 uctrl->connected = true;
266 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
271 /* wait for peer message handling or disconnect */
273 struct pollfd pfd[1];
276 pfd[0].fd = uctrl->sock;
277 pfd[0].events = POLLIN;
278 r = poll(pfd, 1, timeout * 1000);
286 if (r > 0 && pfd[0].revents & POLLERR) {
299 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
301 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
304 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
306 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
309 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
311 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
314 int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout)
316 return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
319 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
321 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
324 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
326 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
329 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
331 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
334 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
336 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
339 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
341 struct udev_ctrl_msg *uctrl_msg;
344 struct cmsghdr *cmsg;
347 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
349 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
350 if (uctrl_msg == NULL)
352 uctrl_msg->refcount = 1;
353 uctrl_msg->conn = conn;
354 udev_ctrl_connection_ref(conn);
356 /* wait for the incoming message */
358 struct pollfd pfd[1];
361 pfd[0].fd = conn->sock;
362 pfd[0].events = POLLIN;
364 r = poll(pfd, 1, 10000);
370 log_error("timeout waiting for ctrl message\n");
373 if (!(pfd[0].revents & POLLIN)) {
374 log_error("ctrl connection error: %m\n");
382 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
383 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
384 memset(&smsg, 0x00, sizeof(struct msghdr));
387 smsg.msg_control = cred_msg;
388 smsg.msg_controllen = sizeof(cred_msg);
389 size = recvmsg(conn->sock, &smsg, 0);
391 log_error("unable to receive ctrl message: %m\n");
394 cmsg = CMSG_FIRSTHDR(&smsg);
395 cred = (struct ucred *) CMSG_DATA(cmsg);
397 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
398 log_error("no sender credentials received, message ignored\n");
402 if (cred->uid != 0) {
403 log_error("sender uid=%i, message ignored\n", cred->uid);
407 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
408 log_error("message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
414 udev_ctrl_msg_unref(uctrl_msg);
418 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
420 if (ctrl_msg == NULL)
422 ctrl_msg->refcount++;
426 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
428 if (ctrl_msg == NULL)
430 ctrl_msg->refcount--;
431 if (ctrl_msg->refcount > 0)
433 udev_ctrl_connection_unref(ctrl_msg->conn);
438 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
440 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
441 return ctrl_msg->ctrl_msg_wire.intval;
445 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
447 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
452 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
454 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
459 int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg)
461 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
466 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
468 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
469 return ctrl_msg->ctrl_msg_wire.buf;
473 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
475 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
476 return ctrl_msg->ctrl_msg_wire.intval;
480 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
482 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
487 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
489 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)