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>
19 #include <sys/socket.h>
23 #include "libudev-private.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,
39 struct udev_ctrl_msg_wire {
42 enum udev_ctrl_msg_type type;
49 struct udev_ctrl_msg {
51 struct udev_ctrl *uctrl;
52 struct udev_ctrl_msg_wire ctrl_msg_wire;
60 struct sockaddr_un saddr;
64 static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
66 struct udev_ctrl *uctrl;
68 uctrl = calloc(1, sizeof(struct udev_ctrl));
76 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
78 struct udev_ctrl *uctrl;
80 uctrl = udev_ctrl_new(udev);
84 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
85 if (uctrl->sock < 0) {
86 err(udev, "error getting socket: %m\n");
87 udev_ctrl_unref(uctrl);
91 uctrl->saddr.sun_family = AF_LOCAL;
92 strcpy(uctrl->saddr.sun_path, socket_path);
93 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
94 /* translate leading '@' to abstract namespace */
95 if (uctrl->saddr.sun_path[0] == '@')
96 uctrl->saddr.sun_path[0] = '\0';
100 struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
102 struct udev_ctrl *uctrl;
104 uctrl = udev_ctrl_new(udev);
112 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
117 if (uctrl->addrlen > 0) {
118 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
120 err(uctrl->udev, "bind failed: %m\n");
125 /* enable receiving of the sender credentials */
126 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
130 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
135 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
143 void udev_ctrl_unref(struct udev_ctrl *uctrl)
148 if (uctrl->refcount > 0)
150 if (uctrl->sock >= 0)
155 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
162 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
164 struct udev_ctrl_msg_wire ctrl_msg_wire;
167 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
168 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
169 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
170 ctrl_msg_wire.type = type;
173 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
175 ctrl_msg_wire.intval = intval;
177 err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
178 (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
180 err(uctrl->udev, "error sending message: %m\n");
185 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
187 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
190 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
192 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
195 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
197 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
200 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
202 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
205 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
207 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
210 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count)
212 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL);
215 int udev_ctrl_send_settle(struct udev_ctrl *uctrl)
217 return ctrl_send(uctrl, UDEV_CTRL_SETTLE, 0, NULL);
220 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
222 struct udev_ctrl_msg *uctrl_msg;
225 struct cmsghdr *cmsg;
228 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
230 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
231 if (uctrl_msg == NULL)
233 uctrl_msg->refcount = 1;
234 uctrl_msg->uctrl = uctrl;
236 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
237 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
239 memset(&smsg, 0x00, sizeof(struct msghdr));
242 smsg.msg_control = cred_msg;
243 smsg.msg_controllen = sizeof(cred_msg);
245 size = recvmsg(uctrl->sock, &smsg, 0);
247 err(uctrl->udev, "unable to receive user udevd message: %m\n");
250 cmsg = CMSG_FIRSTHDR(&smsg);
251 cred = (struct ucred *) CMSG_DATA(cmsg);
253 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
254 err(uctrl->udev, "no sender credentials received, message ignored\n");
258 if (cred->uid != 0) {
259 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
263 uctrl_msg->pid = cred->pid;
265 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
266 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
270 dbg(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
273 udev_ctrl_msg_unref(uctrl_msg);
277 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
279 if (ctrl_msg == NULL)
281 ctrl_msg->refcount++;
285 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
287 if (ctrl_msg == NULL)
289 ctrl_msg->refcount--;
290 if (ctrl_msg->refcount > 0)
292 dbg(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
296 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
298 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
299 return ctrl_msg->ctrl_msg_wire.intval;
303 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
305 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
310 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
312 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
317 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
319 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
324 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
326 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
327 return ctrl_msg->ctrl_msg_wire.buf;
331 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
333 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
334 return ctrl_msg->ctrl_msg_wire.intval;
338 pid_t udev_ctrl_get_settle(struct udev_ctrl_msg *ctrl_msg)
340 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SETTLE)
341 return ctrl_msg->pid;