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_MAX_CHILDS,
36 UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
40 struct udev_ctrl_msg_wire {
43 enum udev_ctrl_msg_type type;
50 struct udev_ctrl_msg {
52 struct udev_ctrl *uctrl;
53 struct udev_ctrl_msg_wire ctrl_msg_wire;
61 struct sockaddr_un saddr;
65 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
67 struct udev_ctrl *uctrl;
69 uctrl = calloc(1, sizeof(struct udev_ctrl));
75 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
76 if (uctrl->sock < 0) {
77 err(udev, "error getting socket: %m\n");
78 udev_ctrl_unref(uctrl);
82 uctrl->saddr.sun_family = AF_LOCAL;
83 strcpy(uctrl->saddr.sun_path, socket_path);
84 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
85 /* translate leading '@' to abstract namespace */
86 if (uctrl->saddr.sun_path[0] == '@')
87 uctrl->saddr.sun_path[0] = '\0';
92 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
95 const int feature_on = 1;
97 err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
99 err(uctrl->udev, "bind failed: %m\n");
103 /* enable receiving of the sender credentials */
104 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
108 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
113 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
121 void udev_ctrl_unref(struct udev_ctrl *uctrl)
126 if (uctrl->refcount > 0)
128 if (uctrl->sock >= 0)
133 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
140 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
142 struct udev_ctrl_msg_wire ctrl_msg_wire;
145 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
146 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
147 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
148 ctrl_msg_wire.type = type;
151 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
153 ctrl_msg_wire.intval = intval;
155 err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
156 (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
158 err(uctrl->udev, "error sending message: %m\n");
163 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
165 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
168 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
170 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
173 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
175 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
178 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
180 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
183 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
185 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
188 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
190 return ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
193 int udev_ctrl_send_settle(struct udev_ctrl *uctrl)
195 return ctrl_send(uctrl, UDEV_CTRL_SETTLE, 0, NULL);
198 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
200 struct udev_ctrl_msg *uctrl_msg;
203 struct cmsghdr *cmsg;
206 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
208 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
209 if (uctrl_msg == NULL)
211 uctrl_msg->refcount = 1;
212 uctrl_msg->uctrl = uctrl;
214 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
215 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
217 memset(&smsg, 0x00, sizeof(struct msghdr));
220 smsg.msg_control = cred_msg;
221 smsg.msg_controllen = sizeof(cred_msg);
223 size = recvmsg(uctrl->sock, &smsg, 0);
225 err(uctrl->udev, "unable to receive user udevd message: %m\n");
228 cmsg = CMSG_FIRSTHDR(&smsg);
229 cred = (struct ucred *) CMSG_DATA(cmsg);
231 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
232 err(uctrl->udev, "no sender credentials received, message ignored\n");
236 if (cred->uid != 0) {
237 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
241 uctrl_msg->pid = cred->pid;
243 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
244 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
248 dbg(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
251 udev_ctrl_msg_unref(uctrl_msg);
255 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
257 if (ctrl_msg == NULL)
259 ctrl_msg->refcount++;
263 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
265 if (ctrl_msg == NULL)
267 ctrl_msg->refcount--;
268 if (ctrl_msg->refcount > 0)
270 dbg(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
274 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
276 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
277 return ctrl_msg->ctrl_msg_wire.intval;
281 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
283 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
288 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
290 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
295 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
297 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
302 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
304 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
305 return ctrl_msg->ctrl_msg_wire.buf;
309 int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
311 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_MAX_CHILDS)
312 return ctrl_msg->ctrl_msg_wire.intval;
316 pid_t udev_ctrl_get_settle(struct udev_ctrl_msg *ctrl_msg)
318 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SETTLE)
319 return ctrl_msg->pid;