I want to create a new socket file in /dev
. Does the way to get it is adding a new rule to udev
?
Ubuntu – How to create a new socket in /dev
application-developmentprogrammingsocketsudev
Related Solutions
Solution without Udev Rules
There is a chance that video4linux has already provided you with stable device names. You may want to check the directories /dev/v4l/by-id/
and /dev/v4l/by-path/
for this. Connect your devices to different USB ports and compare the created device names to make sure that they are really stable.
Solution with Udev Rules
Otherwise it depends on whether your two seemingly identical devices do at least have a different serial number which is accessible by udev. You can find this out by using the correct “Bus” and “Device” number from your lsusb
output in the following command:
udevadm info --attribute-walk /dev/bus/usb/003/005
If there is some kind of output like the following in the first block of key/value pairs and the serial is different between the two devices, then you may use that in your udev rules:
ATTR{serial}=="68974689267119892"
Your udev rules could then look like this:
SUBSYSTEM=="usb", ATTR{serial}=="68974689267119892", ATTR{idVendor}=="1871", ATTR{idProduct}=="0101", NAME="video10"
SUBSYSTEM=="usb", ATTR{serial}=="12345698798725654", ATTR{idVendor}=="1871", ATTR{idProduct}=="0101", NAME="video11"
SUBSYSTEM=="usb", ATTR{idVendor}=="046d", ATTR{idProduct}=="0807", NAME="video12"
If there is no serial (or similar unique) number, then I only see the following possibility: you would have to make sure that your devices always remain connected at the same USB ports. In that case you could use rules like these:
SUBSYSTEM=="usb", KERNEL=="2-3", ATTR{idVendor}=="1871", ATTR{idProduct}=="0101", NAME="video10"
SUBSYSTEM=="usb", KERNEL=="2-4", ATTR{idVendor}=="1871", ATTR{idProduct}=="0101", NAME="video11"
SUBSYSTEM=="usb", KERNEL=="2-2", ATTR{idVendor}=="046d", ATTR{idProduct}=="0807", NAME="video12"
You would have to find out which KERNEL
IDs to use by again running the udevadm
command mentioned above and looking for the KERNEL
key/value pair.
Minor Notes
It may be nicer/better to only create new symlinks instead of new device names, and maybe even grouping them under a common directory:
SUBSYSTEM=="usb", KERNEL=="2-3", ATTR{idVendor}=="1871", ATTR{idProduct}=="0101", SYMLINK+="foo/video10"
The previous rule, for example, would create a device symlink at /dev/foo/video10
.
I would probably also simply name the rules file name-video-devices.rules
, i.e., leaving the leading number out. It probably doesn’t matter when the rules are executed, so no need to use a (rather high) priority of 25.
Few points
Used
75-
, the rules run in order. There is a chance that your rule may overridden by next rules from/lib/udev/rules.d/
, prefer to use99-
~90-
SUBSYSTEM=="usb", SYMLINK+="myusb"
too genericIt match all event actions
add
,remove
&change
. Beside all USB devices. May be you should make it more strict.Check using
lsusb
Bus 003 Device 003: ID 0461:4d81 Primax Electronics, Ltd Dell N889 Optical Mouse
and add them:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d81", SYMLINK+="myusb"
Not all devices create a node in
/dev
. (Could you mention what type of devices you have used)Bonus :), even so the rule still broad concerning tree device nodes. When you plug camera there is not only one node created but many from tree root to the leaf (leaf is in most cases the wanted one). Check:
udevadm info -a /dev/video0
The previous modified rule may match two last nodes (not the leaf).
Best Answer
mknod
command will not allow you to create a socket.From the command line, use the socket package
If you wish to create a Unix/TCP socket from the command-line, try the
socket
command from the socket package (install it first). Summarizing its man page description:Examples of socket usage (from man page)
socket -v coma.cs.tu-berlin.de nntp
connects to the nntp port (port 119) of coma.cs.tu-berlin.de (130.149.28.10).
socket -sl 3425
creates a server socket on port 3425 on the local host and waits for a connection. After a connection has been closed, a new connection is accepted.
socket -wslqvp "echo Socket! " 1938
creates a server socket on port 1938 on the local host and waits for a connection. When a connection is accepted, the string "Socket!" is written to the socket. No data is read from the socket and written to the finger program. The connection is closed when an end-of-file condition at the standard output of the program occurs. Then a new connection is accepted.