How to write basic udev rules and understand their basic concepts and logic in Linux

Reader object

Understand the basic concepts behind udev and learn how to write simple rules.

Claim

Root permission

Difficult

medium

Claim

# – Requires the given command to use root privileges or run directly as a root user or with the sudo command.

$ – requires the given command to run as a normal, non-privileged user.

Introduction

In GNU/Linux systems, although the underlying support for devices is handled at the kernel level, their associated event management is managed by udev in user space. Rather, it is done by the udevd daemon. Learning how to write rules and applying them to these events will help us modify the behavior of the system and adapt it to our needs.

How rules are organized

The udev rule is defined in a file with a .rules extension. Those files are mainly placed in two places: /usr/lib/udev/rules.d, which is used to store system installation rules; /etc/udev/rules.d/ This directory is reserved for custom rules.

The naming convention for files that define those rules is prefixed with a number (for example, 50-udev-default.rules) and processed in the lexical order of the directories in the directory. Files installed in /etc/udev/rules.d will overwrite files of the same name installed in the system default path.

Rule grammar

If you understand the behavioral logic of a udev rule, its syntax is not complicated. A rule consists of two main sections: the match part, which uses a series of comma-separated keys to define the conditions applied by the rule, and the action part, when the condition is met, we perform some actions.

How to write basic udev rules and understand their basic concepts and logic in Linux

Test Case

The best way to explain the possible options is to configure a real case, so let's define a rule as a demo to disable the touchpad when the mouse is connected. Obviously, the properties provided in the rule definition will reflect my hardware.

We will write our rules in our /etc/udev/rules.d/99-togglemouse.rules file with our favorite text editor. A rule definition allows multiple rows to be spanned, but if this is the case, a backslash (\) must be used before a newline character to indicate the continuation of the line, just like a shell script. This is our rule:

ACTION=="add"\

, ATTRS{idProduct}=="c52f"\

, ATTRS{idVendor}=="046d"\

,ENV{DISPLAY}=":0"\

,ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority"\

,RUN+="/usr/bin/xinput --disable 16"

Let's analyze this rule.

Operator

First, explain the operators that have been used and will be used as follows:

== and != operators

== is an equality operator, and != is not equal to an operator. By using them, we can confirm that the keys applied on the rules match their respective values.

Assign operator = and :=

= is an assignment operator that is used to assign a value to a key. When we want to assign values ​​and want to make sure that it is not overwritten by other rules, we need to use the := operator instead, and with the value assigned by this operator, it cannot be changed.

+= and -= operators

The += and -= operators are each used to add or remove a value from a list of values ​​defined by a specified key.

The key we use

Now, let's analyze the keys we use in this rule. First, we have an ACTION key: By using it, when a specific event occurs on a device, we will specify the specific content of the rule we want to apply. Valid values ​​are add, remove, and change.

Then, we use the ATTRS keyword to specify an attribute to match. We can use the udevadm info command to list a device property, provide its name or the sysfs path:

Udevadm info -ap /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1/0003:046D:C52F.0010/input/input39

Udevadm info starts with the device specified by the devpath andthen

Walks up the chain of parentdevices.It prints forevery device

Found,all possible attributes inthe udev rules key format.

Arule tomatch,can be composed by the attributes of the device

Andthe attributes from one single parentdevice.

Looking at device'/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1/0003:046D:C52F.0010/input/input39':

KERNEL=="input39"

SUBSYSTEM=="input"

DRIVER==""

ATTR{name}=="Logitech USB Receiver"

ATTR{phys}=="usb-0000:00:1d.0-1.2/input1"

ATTR{properties}=="0"

ATTR{uniq}==""

Looking at parentdevice'/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1/0003:046D:C52F.0010':

KERNELS=="0003:046D:C52F.0010"

SUBSYSTEMS=="hid"

DRIVERS=="hid-generic"

ATTRS{country}=="00"

Looking at parentdevice'/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1':

KERNELS=="2-1.2:1.1"

SUBSYSTEMS=="usb"

DRIVERS=="usbhid"

ATTRS{authorized}=="1"

ATTRS{bAlternateSetting}==" 0"

ATTRS{bInterfaceClass}=="03"

ATTRS{bInterfaceNumber}=="01"

ATTRS{bInterfaceProtocol}=="00"

ATTRS{bInterfaceSubClass}=="00"

ATTRS{bNumEndpoints}=="01"

ATTRS{supports_autosuspend}=="1"

Looking at parentdevice'/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2':

KERNELS=="2-1.2"

SUBSYSTEMS=="usb"

DRIVERS=="usb"

ATTRS{authorized}=="1"

ATTRS{avoid_reset_quirk}=="0"

ATTRS{bConfigurationValue}=="1"

ATTRS{bDeviceClass}=="00"

ATTRS{bDeviceProtocol}=="00"

ATTRS{bDeviceSubClass}=="00"

ATTRS{bMaxPacketSize0}=="8"

ATTRS{bMaxPower}=="98mA"

ATTRS{bNumConfigurations}=="1"

ATTRS{bNumInterfaces}==" 2"

ATTRS{bcdDevice}=="3000"

ATTRS{bmAttributes}=="a0"

ATTRS{busnum}=="2"

ATTRS{configuration}=="RQR30.00_B0009"

ATTRS{devnum}=="12"

ATTRS{devpath}=="1.2"

ATTRS{idProduct}=="c52f"

ATTRS{idVendor}=="046d"

ATTRS{ltm_capable}=="no"

ATTRS{manufacturer}=="Logitech"

ATTRS{maxchild}=="0"

ATTRS{product}=="USB Receiver"

ATTRS{quirks}=="0x0"

ATTRS{removable}=="removable"

ATTRS{speed}=="12"

ATTRS{urbnum}=="1401"

ATTRS{version}==" 2.00"

[...]

The above captures a portion of the output after running this command. As you can see from its output, udevadm starts with the specified path we provide and provides information about all the parent devices. Note that the properties of the device are reported in singular form (for example, KERNEL), and its parent is in the plural (for example, KERNELS). Parent information can be used as part of a rule, but only one parent can be referenced at a time: the properties of different parent devices do not work together. In the rules we defined above, we used a parent device attribute: idProduct and idVendor.

The next thing to do in our rules is to use the ENV keyword: it can be used both for setting and for matching environment variables. We assign values ​​to DISPLAY and XAUTHORITY. These variables are necessary when we use the X server program to interact to set some of the required information: using the DISPLAY variable, we specify which machine the server is running on, which display and screen is used; a file is provided using XAUTHORITY Path that contains Xorg authentication and authorization information. This file is usually located in the user's home directory.

Finally, we used the RUN word: it is used to run external programs. Very important: there is no immediate run here, but once all the rules are parsed, various actions will be run. In this case, we use the xinput utility to change the state of the touchpad. I don't want to explain the syntax of xinput here, it is beyond the scope of this article, just note that the ID of this touchpad is 16.

After the rule is set, we can debug it by using the udevadm test command. This command is very useful for debugging, it is not really running the command specified by RUN:

$udevadm test --action="add" /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.1/0003:046D:C52F.0010/input /input39

The commands we provide to the command are the --action option, and the mock action of the device's sysfs path. If no errors are reported, our rules are working fine. To use it in a real environment, we need to reload the rules:

# udevadm control --reload

This command will reload the rules file, however, it will only be effective for events that occur after reloading.

We learned the basic concepts and logic by creating a udev rule, which is just a small part of the many options and possible settings in udev rules. The udev man page provides an exhaustive list, so if you want to learn more, please refer to it.

MT6-Subminiature Sealed Micro Switch

Features

â—† Designed For Water and Dust Tight(IP67)

â—† Small Compact Size
â—† UL&ENEC&CQC Safety Approvals
â—† Long life & high reliability
â—† Variety of Levers
â—† Wide Range of wiring Terminals
â—† Wide used in Automotive Electronics,Appliance and Industrial Control etc.

â—† Customized Designs


Safety Micro Switch,Central Locking Switch,Sealed Waterproof Micro Switch,Subminiature Sealed Micro Switch

Ningbo Jialin Electronics Co.,Ltd , https://www.donghai-switch.com