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>
20 #include <sys/socket.h>
24 #include "libudev-private.h"
26 /* wire protocol magic must match */
27 #define UDEV_CTRL_MAGIC 0xdead1dea
29 enum udev_ctrl_msg_type {
31 UDEV_CTRL_SET_LOG_LEVEL,
32 UDEV_CTRL_STOP_EXEC_QUEUE,
33 UDEV_CTRL_START_EXEC_QUEUE,
34 UDEV_CTRL_RELOAD_RULES,
36 UDEV_CTRL_SET_CHILDREN_MAX,
41 struct udev_ctrl_msg_wire {
44 enum udev_ctrl_msg_type type;
51 struct udev_ctrl_msg {
53 struct udev_ctrl_connection *conn;
54 struct udev_ctrl_msg_wire ctrl_msg_wire;
61 struct sockaddr_un saddr;
67 struct udev_ctrl_connection {
69 struct udev_ctrl *uctrl;
73 static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
75 struct udev_ctrl *uctrl;
77 uctrl = calloc(1, sizeof(struct udev_ctrl));
85 struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
87 struct udev_ctrl *uctrl;
89 uctrl = udev_ctrl_new(udev);
94 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
95 if (uctrl->sock < 0) {
96 err(udev, "error getting socket: %m\n");
97 udev_ctrl_unref(uctrl);
105 uctrl->saddr.sun_family = AF_LOCAL;
106 strcpy(uctrl->saddr.sun_path, socket_path);
107 uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
108 /* translate leading '@' to abstract namespace */
109 if (uctrl->saddr.sun_path[0] == '@')
110 uctrl->saddr.sun_path[0] = '\0';
114 struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
116 return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
119 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
124 err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
127 err(uctrl->udev, "bind failed: %m\n");
131 err = listen(uctrl->sock, 0);
134 err(uctrl->udev, "listen failed: %m\n");
143 struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
148 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
156 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
161 if (uctrl->refcount > 0)
163 if (uctrl->sock >= 0)
169 int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
176 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
178 struct udev_ctrl_connection *conn;
181 conn = calloc(1, sizeof(struct udev_ctrl_connection));
187 conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC);
188 if (conn->sock < 0) {
193 /* enable receiving of the sender credentials */
194 setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
195 udev_ctrl_ref(uctrl);
199 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
207 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn)
212 if (conn->refcount > 0)
216 udev_ctrl_unref(conn->uctrl);
221 static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout)
223 struct udev_ctrl_msg_wire ctrl_msg_wire;
226 memset(&ctrl_msg_wire, 0x00, sizeof(struct udev_ctrl_msg_wire));
227 strcpy(ctrl_msg_wire.version, "udev-" VERSION);
228 ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
229 ctrl_msg_wire.type = type;
232 util_strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
234 ctrl_msg_wire.intval = intval;
236 if (!uctrl->connected) {
237 if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
241 uctrl->connected = true;
243 if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
248 /* wait for peer message handling or disconnect */
250 struct pollfd pfd[1];
253 pfd[0].fd = uctrl->sock;
254 pfd[0].events = POLLIN;
255 r = poll(pfd, 1, timeout * 1000);
263 if (r > 0 && pfd[0].revents & POLLERR) {
276 int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout)
278 return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
281 int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout)
283 return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
286 int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout)
288 return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
291 int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl, int timeout)
293 return ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL, timeout);
296 int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout)
298 return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
301 int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout)
303 return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
306 int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout)
308 return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
311 int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout)
313 return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
316 struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
318 struct udev *udev = conn->uctrl->udev;
319 struct udev_ctrl_msg *uctrl_msg;
322 struct cmsghdr *cmsg;
325 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
327 uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
328 if (uctrl_msg == NULL)
330 uctrl_msg->refcount = 1;
331 uctrl_msg->conn = conn;
332 udev_ctrl_connection_ref(conn);
334 iov.iov_base = &uctrl_msg->ctrl_msg_wire;
335 iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
336 memset(&smsg, 0x00, sizeof(struct msghdr));
339 smsg.msg_control = cred_msg;
340 smsg.msg_controllen = sizeof(cred_msg);
341 size = recvmsg(conn->sock, &smsg, 0);
343 err(udev, "unable to receive user udevd message: %m\n");
346 cmsg = CMSG_FIRSTHDR(&smsg);
347 cred = (struct ucred *) CMSG_DATA(cmsg);
349 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
350 err(udev, "no sender credentials received, message ignored\n");
354 if (cred->uid != 0) {
355 err(udev, "sender uid=%i, message ignored\n", cred->uid);
359 if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
360 err(udev, "message magic 0x%08x doesn't match, ignore it\n", uctrl_msg->ctrl_msg_wire.magic);
364 dbg(udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg_wire.type);
367 udev_ctrl_msg_unref(uctrl_msg);
371 struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
373 if (ctrl_msg == NULL)
375 ctrl_msg->refcount++;
379 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
381 if (ctrl_msg == NULL)
383 ctrl_msg->refcount--;
384 if (ctrl_msg->refcount > 0)
386 dbg(ctrl_msg->conn->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
387 udev_ctrl_connection_unref(ctrl_msg->conn);
392 int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
394 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
395 return ctrl_msg->ctrl_msg_wire.intval;
399 int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
401 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
406 int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
408 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
413 int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
415 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD_RULES)
420 const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
422 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
423 return ctrl_msg->ctrl_msg_wire.buf;
427 int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg)
429 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
430 return ctrl_msg->ctrl_msg_wire.intval;
434 int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg)
436 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
441 int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg)
443 if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)