2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include "libudev-private.h"
33 #define UDEV_CTRL_MAGIC "udevd-128"
35 enum udev_ctrl_msg_type {
37 UDEV_CTRL_SET_LOG_LEVEL,
38 UDEV_CTRL_STOP_EXEC_QUEUE,
39 UDEV_CTRL_START_EXEC_QUEUE,
40 UDEV_CTRL_RELOAD_RULES,
42 UDEV_CTRL_SET_MAX_CHILDS,
43 UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
46 struct ctrl_msg_wire {
48 enum udev_ctrl_msg_type type;
55 struct udev_ctrl_msg {
57 struct udev_ctrl *uctrl;
58 struct ctrl_msg_wire ctrl_msg_wire;
65 struct sockaddr_un saddr;
69 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
71 struct udev_ctrl *uctrl;
73 uctrl = malloc(sizeof(struct udev_ctrl));
76 memset(uctrl, 0x00, sizeof(struct udev_ctrl));
80 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
81 if (uctrl->sock < 0) {
82 err(udev, "error getting socket: %m\n");
83 udev_ctrl_unref(uctrl);
87 uctrl->saddr.sun_family = AF_LOCAL;
88 strcpy(uctrl->saddr.sun_path, socket_path);
89 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
90 /* translate leading '@' to abstract namespace */
91 if (uctrl->saddr.sun_path[0] == '@')
92 uctrl->saddr.sun_path[0] = '\0';
97 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
100 const int feature_on = 1;
102 err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
104 err(uctrl->udev, "bind failed: %m\n");
108 /* enable receiving of the sender credentials */
109 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
113 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
118 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
126 void udev_ctrl_unref(struct udev_ctrl *uctrl)
131 if (uctrl->refcount > 0)
133 if (uctrl->sock >= 0)
138 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
145 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
147 struct ctrl_msg_wire ctrl_msg_wire;
150 memset(&ctrl_msg_wire, 0x00, sizeof(struct ctrl_msg_wire));
151 strcpy(ctrl_msg_wire.magic, UDEV_CTRL_MAGIC);
152 ctrl_msg_wire.type = type;
155 util_strlcpy(ctrl_msg_wire.buf, buf, sizeof(ctrl_msg_wire.buf));
157 ctrl_msg_wire.intval = intval;
159 err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
161 err(uctrl->udev, "error sending message: %m\n");
166 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
168 ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
172 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
174 ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
178 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
180 ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
184 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
186 ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
190 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
192 ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, optarg);
196 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
198 ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
202 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
204 struct udev_ctrl_msg *uctrl_msg;
207 struct cmsghdr *cmsg;
210 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
212 uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
213 if (uctrl_msg == NULL)
215 memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
216 uctrl_msg->refcount = 1;
217 uctrl_msg->uctrl = uctrl;
219 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
220 iov.iov_len = sizeof(struct udev_ctrl_msg);
222 memset(&smsg, 0x00, sizeof(struct msghdr));
225 smsg.msg_control = cred_msg;
226 smsg.msg_controllen = sizeof(cred_msg);
228 size = recvmsg(uctrl->sock, &smsg, 0);
230 err(uctrl->udev, "unable to receive user udevd message: %m\n");
233 cmsg = CMSG_FIRSTHDR(&smsg);
234 cred = (struct ucred *) CMSG_DATA(cmsg);
236 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
237 err(uctrl->udev, "no sender credentials received, message ignored\n");
241 if (cred->uid != 0) {
242 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
246 if (strncmp(uctrl_msg->ctrl_msg_wire.magic, UDEV_CTRL_MAGIC, sizeof(UDEV_CTRL_MAGIC)) != 0 ) {
247 err(uctrl->udev, "message magic '%s' doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
251 info(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
254 udev_ctrl_msg_unref(uctrl_msg);
258 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
260 if (ctrl_msg == NULL)
262 ctrl_msg->refcount++;
266 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
268 if (ctrl_msg == NULL)
270 ctrl_msg->refcount--;
271 if (ctrl_msg->refcount > 0)
273 info(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
277 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
279 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
280 return ctrl_msg->ctrl_msg_wire.intval;
284 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
286 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
291 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
293 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
298 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
300 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
305 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
307 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
308 return ctrl_msg->ctrl_msg_wire.buf;
312 int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
314 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_MAX_CHILDS)
315 return ctrl_msg->ctrl_msg_wire.intval;