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>
24 #include "libudev-private.h"
26 /* wire protocol magic must match */
27 #define UDEV_CTRL_MAGIC 0xdead1dea
29 enum udev_ctrl_msg_type {
31 UDEV_CTRL_SET_LOG_LEVEL,
32 UDEV_CTRL_STOP_EXEC_QUEUE,
33 UDEV_CTRL_START_EXEC_QUEUE,
34 UDEV_CTRL_RELOAD_RULES,
36 UDEV_CTRL_SET_CHILDREN_MAX,
41 struct udev_ctrl_msg_wire {
44 enum udev_ctrl_msg_type type;
51 struct udev_ctrl_msg {
53 struct udev_ctrl_connection *conn;
54 struct udev_ctrl_msg_wire ctrl_msg_wire;
61 struct sockaddr_un saddr;
67 struct udev_ctrl_connection {
69 struct udev_ctrl *uctrl;
73 static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
75 struct udev_ctrl *uctrl;
77 uctrl = calloc(1, sizeof(struct udev_ctrl));
85 struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
87 struct udev_ctrl *uctrl;
89 uctrl = udev_ctrl_new(udev);
94 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
95 if (uctrl->sock < 0) {
96 err(udev, "error getting socket: %m\n");
97 udev_ctrl_unref(uctrl);
105 uctrl->saddr.sun_family = AF_LOCAL;
106 strcpy(uctrl->saddr.sun_path, socket_path);
107 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
108 /* translate leading '@' to abstract namespace */
109 if (uctrl->saddr.sun_path[0] == '@')
110 uctrl->saddr.sun_path[0] = '\0';
114 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
116 return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
119 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
124 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
127 err(uctrl->udev, "bind failed: %m\n");
131 err = listen(uctrl->sock, 0);
134 err(uctrl->udev, "listen failed: %m\n");
143 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
148 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
156 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
161 if (uctrl->refcount > 0)
163 if (uctrl->sock >= 0)
169 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
176 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
178 struct udev_ctrl_connection *conn;
183 conn = calloc(1, sizeof(struct udev_ctrl_connection));
189 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
190 if (conn->sock < 0) {
192 err(uctrl->udev, "unable to receive ctrl connection: %m\n");
196 /* check peer credential of connection */
197 slen = sizeof(ucred);
198 if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
199 err(uctrl->udev, "unable to receive credentials of ctrl connection: %m\n");
203 err(uctrl->udev, "sender uid=%i, message ignored\n", ucred.uid);
207 /* enable receiving of the sender credentials in the messages */
208 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
209 udev_ctrl_ref(uctrl);
218 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
226 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
231 if (conn->refcount > 0)
235 udev_ctrl_unref(conn->uctrl);
240 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
242 struct udev_ctrl_msg_wire ctrl_msg_wire;
245 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
246 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
247 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
248 ctrl_msg_wire.type = type;
251 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
253 ctrl_msg_wire.intval = intval;
255 if (!uctrl->connected) {
256 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
260 uctrl->connected = true;
262 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
267 /* wait for peer message handling or disconnect */
269 struct pollfd pfd[1];
272 pfd[0].fd = uctrl->sock;
273 pfd[0].events = POLLIN;
274 r = poll(pfd, 1, timeout * 1000);
282 if (r > 0 && pfd[0].revents & POLLERR) {
295 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
297 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
300 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
302 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
305 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
307 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
310 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
312 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
315 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
317 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
320 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
322 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
325 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
327 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
330 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
332 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
335 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
337 struct udev *udev = conn->uctrl->udev;
338 struct udev_ctrl_msg *uctrl_msg;
341 struct cmsghdr *cmsg;
344 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
346 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
347 if (uctrl_msg == NULL)
349 uctrl_msg->refcount = 1;
350 uctrl_msg->conn = conn;
351 udev_ctrl_connection_ref(conn);
353 /* wait for the incoming message */
355 struct pollfd pfd[1];
358 pfd[0].fd = conn->sock;
359 pfd[0].events = POLLIN;
361 r = poll(pfd, 1, 10000);
367 err(udev, "timeout waiting for ctrl message\n");
370 if (!(pfd[0].revents & POLLIN)) {
371 err(udev, "ctrl connection error: %m\n");
379 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
380 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
381 memset(&smsg, 0x00, sizeof(struct msghdr));
384 smsg.msg_control = cred_msg;
385 smsg.msg_controllen = sizeof(cred_msg);
386 size = recvmsg(conn->sock, &smsg, 0);
388 err(udev, "unable to receive ctrl message: %m\n");
391 cmsg = CMSG_FIRSTHDR(&smsg);
392 cred = (struct ucred *) CMSG_DATA(cmsg);
394 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
395 err(udev, "no sender credentials received, message ignored\n");
399 if (cred->uid != 0) {
400 err(udev, "sender uid=%i, message ignored\n", cred->uid);
404 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
405 err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
409 dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
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 dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
432 udev_ctrl_connection_unref(ctrl_msg->conn);
437 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
439 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
440 return ctrl_msg->ctrl_msg_wire.intval;
444 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
446 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
451 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
453 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
458 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
460 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
465 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
467 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
468 return ctrl_msg->ctrl_msg_wire.buf;
472 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
474 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
475 return ctrl_msg->ctrl_msg_wire.intval;
479 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
481 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
486 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
488 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)