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 /* last known version with this wire protocol */
34 #define UDEV_CTRL_MAGIC 0xdead1dea
36 enum udev_ctrl_msg_type {
38 UDEV_CTRL_SET_LOG_LEVEL,
39 UDEV_CTRL_STOP_EXEC_QUEUE,
40 UDEV_CTRL_START_EXEC_QUEUE,
41 UDEV_CTRL_RELOAD_RULES,
43 UDEV_CTRL_SET_MAX_CHILDS,
44 UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
47 struct udev_ctrl_msg_wire {
50 enum udev_ctrl_msg_type type;
57 struct udev_ctrl_msg {
59 struct udev_ctrl *uctrl;
60 struct udev_ctrl_msg_wire ctrl_msg_wire;
67 struct sockaddr_un saddr;
71 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
73 struct udev_ctrl *uctrl;
75 uctrl = calloc(1, sizeof(struct udev_ctrl));
81 uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
82 if (uctrl->sock < 0) {
83 err(udev, "error getting socket: %m\n");
84 udev_ctrl_unref(uctrl);
88 uctrl->saddr.sun_family = AF_LOCAL;
89 strcpy(uctrl->saddr.sun_path, socket_path);
90 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
91 /* translate leading '@' to abstract namespace */
92 if (uctrl->saddr.sun_path[0] == '@')
93 uctrl->saddr.sun_path[0] = '\0';
98 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
101 const int feature_on = 1;
103 err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
105 err(uctrl->udev, "bind failed: %m\n");
109 /* enable receiving of the sender credentials */
110 setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
114 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
119 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
127 void udev_ctrl_unref(struct udev_ctrl *uctrl)
132 if (uctrl->refcount > 0)
134 if (uctrl->sock >= 0)
139 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
146 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
148 struct udev_ctrl_msg_wire ctrl_msg_wire;
151 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
152 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
153 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
154 ctrl_msg_wire.type = type;
157 util_strlcpy(ctrl_msg_wire.buf, buf, sizeof(ctrl_msg_wire.buf));
159 ctrl_msg_wire.intval = intval;
161 err = sendto(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0,
162 (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
164 err(uctrl->udev, "error sending message: %m\n");
169 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
171 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
174 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
176 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
179 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
181 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
184 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
186 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
189 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
191 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key);
194 int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
196 return ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
199 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
201 struct udev_ctrl_msg *uctrl_msg;
204 struct cmsghdr *cmsg;
207 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
209 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
210 if (uctrl_msg == NULL)
212 uctrl_msg->refcount = 1;
213 uctrl_msg->uctrl = uctrl;
215 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
216 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
218 memset(&smsg, 0x00, sizeof(struct msghdr));
221 smsg.msg_control = cred_msg;
222 smsg.msg_controllen = sizeof(cred_msg);
224 size = recvmsg(uctrl->sock, &smsg, 0);
226 err(uctrl->udev, "unable to receive user udevd message: %m\n");
229 cmsg = CMSG_FIRSTHDR(&smsg);
230 cred = (struct ucred *) CMSG_DATA(cmsg);
232 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
233 err(uctrl->udev, "no sender credentials received, message ignored\n");
237 if (cred->uid != 0) {
238 err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
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_max_childs(struct udev_ctrl_msg *ctrl_msg)
310 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_MAX_CHILDS)
311 return ctrl_msg->ctrl_msg_wire.intval;