summaryrefslogtreecommitdiff
path: root/libudev
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2011-04-14 23:46:44 +0200
committerKay Sievers <kay.sievers@vrfy.org>2011-04-14 23:46:44 +0200
commit7459bcdc8d12fdb217efd4a27a509f9f87ec7b1e (patch)
tree37d4efef248ce73af16975c0c20830b1cae54159 /libudev
parent7f2d3a3fc805d6a02a35ac3743139e3ea3a1ce73 (diff)
udevd: get netlink socket from systemd
Diffstat (limited to 'libudev')
-rw-r--r--libudev/libudev-ctrl.c34
-rw-r--r--libudev/libudev-monitor.c91
-rw-r--r--libudev/libudev-private.h3
3 files changed, 75 insertions, 53 deletions
diff --git a/libudev/libudev-ctrl.c b/libudev/libudev-ctrl.c
index 2d2dd3a0be..7fa2d1d535 100644
--- a/libudev/libudev-ctrl.c
+++ b/libudev/libudev-ctrl.c
@@ -60,6 +60,7 @@ struct udev_ctrl {
int sock;
struct sockaddr_un saddr;
socklen_t addrlen;
+ bool bound;
bool connected;
};
@@ -81,7 +82,7 @@ static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
return uctrl;
}
-struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
+struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
{
struct udev_ctrl *uctrl;
@@ -89,11 +90,16 @@ struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socke
if (uctrl == NULL)
return NULL;
- uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
- if (uctrl->sock < 0) {
- err(udev, "error getting socket: %m\n");
- udev_ctrl_unref(uctrl);
- return NULL;
+ if (fd < 0) {
+ uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
+ if (uctrl->sock < 0) {
+ err(udev, "error getting socket: %m\n");
+ udev_ctrl_unref(uctrl);
+ return NULL;
+ }
+ } else {
+ uctrl->bound = true;
+ uctrl->sock = fd;
}
uctrl->saddr.sun_family = AF_LOCAL;
@@ -105,35 +111,31 @@ struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socke
return uctrl;
}
-struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
+struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
{
- struct udev_ctrl *uctrl;
-
- uctrl = udev_ctrl_new(udev);
- if (uctrl == NULL)
- return NULL;
- uctrl->sock = fd;
-
- return uctrl;
+ return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
}
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
{
int err;
- if (uctrl->addrlen > 0) {
+ if (!uctrl->bound) {
err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err < 0) {
err = -errno;
err(uctrl->udev, "bind failed: %m\n");
return err;
}
+
err = listen(uctrl->sock, 0);
if (err < 0) {
err = -errno;
err(uctrl->udev, "listen failed: %m\n");
return err;
}
+
+ uctrl->bound = true;
}
return 0;
}
diff --git a/libudev/libudev-monitor.c b/libudev/libudev-monitor.c
index c97f6faa65..5d9e155722 100644
--- a/libudev/libudev-monitor.c
+++ b/libudev/libudev-monitor.c
@@ -50,6 +50,7 @@ struct udev_monitor {
socklen_t addrlen;
struct udev_list_node filter_subsystem_list;
struct udev_list_node filter_tag_list;
+ bool bound;
};
enum udev_monitor_netlink_group {
@@ -155,32 +156,7 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
return udev_monitor;
}
-/**
- * udev_monitor_new_from_netlink:
- * @udev: udev library context
- * @name: name of event source
- *
- * Create new udev monitor and connect to a specified event
- * source. Valid sources identifiers are "udev" and "kernel".
- *
- * Applications should usually not connect directly to the
- * "kernel" events, because the devices might not be useable
- * at that time, before udev has configured them, and created
- * device nodes.
- *
- * Accessing devices at the same time as udev, might result
- * in unpredictable behavior.
- *
- * The "udev" events are sent out after udev has finished its
- * event processing, all rules have been processed, and needed
- * device nodes are created.
- *
- * The initial refcount is 1, and needs to be decremented to
- * release the resources of the udev monitor.
- *
- * Returns: a new udev monitor, or #NULL, in case of an error
- **/
-struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
+struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
{
struct udev_monitor *udev_monitor;
unsigned int group;
@@ -201,11 +177,16 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char
if (udev_monitor == NULL)
return NULL;
- udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
- if (udev_monitor->sock == -1) {
- err(udev, "error getting socket: %m\n");
- free(udev_monitor);
- return NULL;
+ if (fd < 0) {
+ udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
+ if (udev_monitor->sock == -1) {
+ err(udev, "error getting socket: %m\n");
+ free(udev_monitor);
+ return NULL;
+ }
+ } else {
+ udev_monitor->bound = true;
+ udev_monitor->sock = fd;
}
udev_monitor->snl.nl_family = AF_NETLINK;
@@ -219,6 +200,36 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char
return udev_monitor;
}
+/**
+ * udev_monitor_new_from_netlink:
+ * @udev: udev library context
+ * @name: name of event source
+ *
+ * Create new udev monitor and connect to a specified event
+ * source. Valid sources identifiers are "udev" and "kernel".
+ *
+ * Applications should usually not connect directly to the
+ * "kernel" events, because the devices might not be useable
+ * at that time, before udev has configured them, and created
+ * device nodes.
+ *
+ * Accessing devices at the same time as udev, might result
+ * in unpredictable behavior.
+ *
+ * The "udev" events are sent out after udev has finished its
+ * event processing, all rules have been processed, and needed
+ * device nodes are created.
+ *
+ * The initial refcount is 1, and needs to be decremented to
+ * release the resources of the udev monitor.
+ *
+ * Returns: a new udev monitor, or #NULL, in case of an error
+ **/
+struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
+{
+ return udev_monitor_new_from_netlink_fd(udev, name, -1);
+}
+
static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
unsigned short code, unsigned int data)
{
@@ -364,16 +375,24 @@ int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct
*/
int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
{
- int err;
+ int err = 0;
const int on = 1;
if (udev_monitor->sun.sun_family != 0) {
- err = bind(udev_monitor->sock,
- (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
+ if (!udev_monitor->bound) {
+ err = bind(udev_monitor->sock,
+ (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
+ if (err == 0)
+ udev_monitor->bound = true;
+ }
} else if (udev_monitor->snl.nl_family != 0) {
udev_monitor_filter_update(udev_monitor);
- err = bind(udev_monitor->sock,
- (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
+ if (!udev_monitor->bound) {
+ err = bind(udev_monitor->sock,
+ (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
+ if (err == 0)
+ udev_monitor->bound = true;
+ }
if (err == 0) {
struct sockaddr_nl snl;
socklen_t addrlen;
diff --git a/libudev/libudev-private.h b/libudev/libudev-private.h
index e82ca1d753..63eb0704d6 100644
--- a/libudev/libudev-private.h
+++ b/libudev/libudev-private.h
@@ -122,11 +122,12 @@ int udev_monitor_disconnect(struct udev_monitor *udev_monitor);
int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender);
int udev_monitor_send_device(struct udev_monitor *udev_monitor,
struct udev_monitor *destination, struct udev_device *udev_device);
+struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd);
/* libudev-ctrl.c - daemon runtime setup */
struct udev_ctrl;
struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
-struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd);
+struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd);
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl);
struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl);
struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl);