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,
33 UDEV_CTRL_RELOAD_RULES,
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;
66 struct udev_ctrl_connection {
68 struct udev_ctrl *uctrl;
72 static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
74 struct udev_ctrl *uctrl;
76 uctrl = calloc(1, sizeof(struct udev_ctrl));
84 struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
86 struct udev_ctrl *uctrl;
88 uctrl = udev_ctrl_new(udev);
93 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
94 if (uctrl->sock < 0) {
95 err(udev, "error getting socket: %m\n");
96 udev_ctrl_unref(uctrl);
104 uctrl->saddr.sun_family = AF_LOCAL;
105 strcpy(uctrl->saddr.sun_path, socket_path);
106 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
107 /* translate leading '@' to abstract namespace */
108 if (uctrl->saddr.sun_path[0] == '@')
109 uctrl->saddr.sun_path[0] = '\0';
113 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
115 return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
118 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
123 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
126 err(uctrl->udev, "bind failed: %m\n");
130 err = listen(uctrl->sock, 0);
133 err(uctrl->udev, "listen failed: %m\n");
142 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
147 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
155 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
160 if (uctrl->refcount > 0)
162 if (uctrl->sock >= 0)
168 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
175 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
177 struct udev_ctrl_connection *conn;
182 conn = calloc(1, sizeof(struct udev_ctrl_connection));
188 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
189 if (conn->sock < 0) {
191 err(uctrl->udev, "unable to receive ctrl connection: %m\n");
195 /* check peer credential of connection */
196 slen = sizeof(ucred);
197 if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
198 err(uctrl->udev, "unable to receive credentials of ctrl connection: %m\n");
202 err(uctrl->udev, "sender uid=%i, message ignored\n", ucred.uid);
206 /* enable receiving of the sender credentials in the messages */
207 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
208 udev_ctrl_ref(uctrl);
217 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
225 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
230 if (conn->refcount > 0)
234 udev_ctrl_unref(conn->uctrl);
239 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
241 struct udev_ctrl_msg_wire ctrl_msg_wire;
244 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
245 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
246 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
247 ctrl_msg_wire.type = type;
250 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
252 ctrl_msg_wire.intval = intval;
254 if (!uctrl->connected) {
255 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
259 uctrl->connected = true;
261 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
266 /* wait for peer message handling or disconnect */
268 struct pollfd pfd[1];
271 pfd[0].fd = uctrl->sock;
272 pfd[0].events = POLLIN;
273 r = poll(pfd, 1, timeout * 1000);
281 if (r > 0 && pfd[0].revents & POLLERR) {
294 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
296 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
299 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
301 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
304 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
306 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
309 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
311 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
314 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
316 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
319 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
321 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
324 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
326 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
329 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
331 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
334 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
336 struct udev *udev = conn->uctrl->udev;
337 struct udev_ctrl_msg *uctrl_msg;
340 struct cmsghdr *cmsg;
343 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
345 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
346 if (uctrl_msg == NULL)
348 uctrl_msg->refcount = 1;
349 uctrl_msg->conn = conn;
350 udev_ctrl_connection_ref(conn);
352 /* wait for the incoming message */
354 struct pollfd pfd[1];
357 pfd[0].fd = conn->sock;
358 pfd[0].events = POLLIN;
360 r = poll(pfd, 1, 10000);
366 err(udev, "timeout waiting for ctrl message\n");
369 if (!(pfd[0].revents & POLLIN)) {
370 err(udev, "ctrl connection error: %m\n");
378 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
379 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
380 memset(&smsg, 0x00, sizeof(struct msghdr));
383 smsg.msg_control = cred_msg;
384 smsg.msg_controllen = sizeof(cred_msg);
385 size = recvmsg(conn->sock, &smsg, 0);
387 err(udev, "unable to receive ctrl message: %m\n");
390 cmsg = CMSG_FIRSTHDR(&smsg);
391 cred = (struct ucred *) CMSG_DATA(cmsg);
393 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
394 err(udev, "no sender credentials received, message ignored\n");
398 if (cred->uid != 0) {
399 err(udev, "sender uid=%i, message ignored\n", cred->uid);
403 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
404 err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
408 dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
411 udev_ctrl_msg_unref(uctrl_msg);
415 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
417 if (ctrl_msg == NULL)
419 ctrl_msg->refcount++;
423 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
425 if (ctrl_msg == NULL)
427 ctrl_msg->refcount--;
428 if (ctrl_msg->refcount > 0)
430 dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
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_rules(struct udev_ctrl_msg *ctrl_msg)
459 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
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)