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 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
66 struct udev_ctrl *uctrl;
68 uctrl = calloc(1, sizeof(struct udev_ctrl));
74 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
75 if (uctrl->sock < 0) {
76 err(udev, "error getting socket: %m\n");
77 udev_ctrl_unref(uctrl);
81 uctrl->saddr.sun_family = AF_LOCAL;
82 strcpy(uctrl->saddr.sun_path, socket_path);
83 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
84 /* translate leading '@' to abstract namespace */
85 if (uctrl->saddr.sun_path[0] == '@')
86 uctrl->saddr.sun_path[0] = '\0';
91 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
94 const int feature_on = 1;
96 err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
98 err(uctrl->udev, "bind failed: %m\n");
102 /* enable receiving of the sender credentials */
103 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
107 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
112 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
120 void udev_ctrl_unref(struct udev_ctrl *uctrl)
125 if (uctrl->refcount > 0)
127 if (uctrl->sock >= 0)
132 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
139 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
141 struct udev_ctrl_msg_wire ctrl_msg_wire;
144 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
145 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
146 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
147 ctrl_msg_wire.type = type;
150 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
152 ctrl_msg_wire.intval = intval;
154 err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
155 (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
157 err(uctrl->udev, "error sending message: %m\n");
162 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
164 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
167 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
169 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
172 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
174 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
177 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
179 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
182 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
184 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
187 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count)
189 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL);
192 int udev_ctrl_send_settle(struct udev_ctrl *uctrl)
194 return ctrl_send(uctrl, UDEV_CTRL_SETTLE, 0, NULL);
197 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
199 struct udev_ctrl_msg *uctrl_msg;
202 struct cmsghdr *cmsg;
205 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
207 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
208 if (uctrl_msg == NULL)
210 uctrl_msg->refcount = 1;
211 uctrl_msg->uctrl = uctrl;
213 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
214 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
216 memset(&smsg, 0x00, sizeof(struct msghdr));
219 smsg.msg_control = cred_msg;
220 smsg.msg_controllen = sizeof(cred_msg);
222 size = recvmsg(uctrl->sock, &smsg, 0);
224 err(uctrl->udev, "unable to receive user udevd message: %m\n");
227 cmsg = CMSG_FIRSTHDR(&smsg);
228 cred = (struct ucred *) CMSG_DATA(cmsg);
230 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
231 err(uctrl->udev, "no sender credentials received, message ignored\n");
235 if (cred->uid != 0) {
236 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
240 uctrl_msg->pid = cred->pid;
242 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
243 err(uctrl->udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
247 dbg(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
250 udev_ctrl_msg_unref(uctrl_msg);
254 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
256 if (ctrl_msg == NULL)
258 ctrl_msg->refcount++;
262 void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
264 if (ctrl_msg == NULL)
266 ctrl_msg->refcount--;
267 if (ctrl_msg->refcount > 0)
269 dbg(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
273 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
275 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
276 return ctrl_msg->ctrl_msg_wire.intval;
280 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
282 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
287 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
289 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
294 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
296 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
301 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
303 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
304 return ctrl_msg->ctrl_msg_wire.buf;
308 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
310 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
311 return ctrl_msg->ctrl_msg_wire.intval;
315 pid_t udev_ctrl_get_settle(struct udev_ctrl_msg *ctrl_msg)
317 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SETTLE)
318 return ctrl_msg->pid;