问题
I am a bit confused with the questions listed below:
While I execute
udevadm
on my desktop, it is able to listenuevent
sent from kernel. I think before the execution ofudevadm
, it will check the availability ofudevd
. That means, if theudevd
is not available on my desktop,udevadm
will not be able to work. Is my thinking correct?To have the same functionality of
udevadm
, I found that linux also provides another way
to archive this. It's callednetlink
. What confuses me is If I do things this way, I could have exactly the same thing that I have by usingudevadm
. Hence, what's the difference betweenudev
vs.netlink socket
?
socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
<----The socket I created to listen to uevent
.
Thanks for avd's feedback. I still have some questions to ask after having your feedback.
There are not only
udevd
can listen message from kernel, but alsoudevadm
does. Is my thinking correct? Or udevadm is only to manageudevd
.By setting up the socket binding to the
NETLINK_KOBJECT_UEVENT
, the user space code can also listen uevent sent from kernel. At this point, It seems I have no reason to chooseudev
to complete this function. Is there any different between these two approaches?In user space, Can two different processes listen to
uevent
simultaneously? Cannetlink
send the message to these processes in the same time?
回答1:
- Yes, you're right.
udevadm
is to manageudevd
. - This is where you're really confused. Let me clarify this.
udev
is userspace device manager. udev is responsible for your devices to appear in /dev directory. It's also responsible for hotplug functionality. To make things done udev works by receiving messages from kernel. Kernel sends messages via netlink socket. Netlink is just IPC facility implemented as separate socket family specifically for kernel to userspace interaction. So kernel sends messages of special format (uevent) over netlink socket. On the other site (in userspace) there must be someone who is listening for this messages and that's what udev does. Main part ofudev
is udev daemon -udevd
. That daemon listens for that messages and creates special device files under /dev path and provide to you (linux user) interface to manage devices and hotplug (udev rules).
I've answered related question - check it here.
Extra answers:
From udevadm manpage:
udevadm expects a command and command specific options. It controls the runtime behavior of systemd-udevd, requests kernel events, manages the event queue, and provides simple debugging mechanisms.
So it's just a managing tool though it can requests kernel event on admin command.
(I might not understand you question correctly). You can write your own daemon to listen for uevents. That's what gentoo's mdev does.
Netlink allows you to provide multiple listeners for netlink messages. But it depends on a way that kernel socket sends message (unicast, multicast, broadcast) and netlink family. Netlink itself is a collection of families, so it may depends on what netlink you are using. For example, NETLINK_GENERIC family allows you to bind multiple userspace sockets for messages and you will receive that messages. I think the best way to answer this question is to write some simple listening code (probably with some help of libudev)
来源:https://stackoverflow.com/questions/22952692/udevadm-vs-linux-hotplug