# sd_device_enumerator_add_match_parent

## NAME



           sd_device_enumerator_add_match_parent,
           sd_device_enumerator_add_match_property,
           sd_device_enumerator_add_match_property_required,
           sd_device_enumerator_add_match_subsystem,
           sd_device_enumerator_add_match_sysattr,
           sd_device_enumerator_add_match_sysname,
           sd_device_enumerator_add_nomatch_sysname,
           sd_device_enumerator_add_match_tag,
           sd_device_enumerator_allow_uninitialized,
           sd_device_enumerator_add_all_parents - Add a filter to the device
           enumerator
    


## SYNOPSIS



           **#include <systemd/sd-device.h>**
    
           **int**
                                                     **sd_device_enumerator_add_match_parent(sd_device_enumerator ***_enumerator_**,**
                                                     **sd_device ***_parent_**);**
    
           **int**
                                                       **sd_device_enumerator_add_match_property(sd_device_enumerator ***_enumerator_**,**
                                                       **const char ***_property_**,**
                                                       **const char ***_value_**);**
    
           **int**
                                                                **sd_device_enumerator_add_match_property_required(sd_device_enumerator ***_enumerator_**,**
                                                                **const char ***_property_**,**
                                                                **const char ***_value_**);**
    
           **int**
                                                        **sd_device_enumerator_add_match_subsystem(sd_device_enumerator ***_enumerator_**,**
                                                        **const char ***_subsystem_**,**
                                                        **int** _match_**);**
    
           **int**
                                                      **sd_device_enumerator_add_match_sysattr(sd_device_enumerator ***_enumerator_**,**
                                                      **const char ***_sysattr_**,**
                                                      **const char ***_value_**,**
                                                      **int** _match_**);**
    
           **int**
                                                      **sd_device_enumerator_add_match_sysname(sd_device_enumerator ***_enumerator_**,**
                                                      **const char ***_sysname_**);**
    
           **int**
                                                        **sd_device_enumerator_add_nomatch_sysname(sd_device_enumerator ***_enumerator_**,**
                                                        **const char ***_sysname_**);**
    
           **int**
                                                  **sd_device_enumerator_add_match_tag(sd_device_enumerator ***_enumerator_**,**
                                                  **const char ***_tag_**);**
    
           **int**
                                                        **sd_device_enumerator_allow_uninitialized(sd_device_enumerator ***_enumerator_**);**
    
           **int**
                                                    **sd_device_enumerator_add_all_parents(sd_device_enumerator ***_enumerator_**);**
    


## DESCRIPTION



           The **sd_device_enumerator_add_match_parent()** function adds a filter
           to the _enumerator_ so that only devices under the tree of the
           specified _parent_ device are enumerated. If this is called multiple
           times the previously set _parent_ device is cleared and only the
           last call takes an effect.
    
           The **sd_device_enumerator_add_match_property()** function adds a
           filter to the _enumerator_ so that only devices with the specified
           _property_ equals to the _value_ are enumerated. Both _property_ and
           _value_ can be a glob pattern. When this is called multiple times,
           devices that have at least one of the specified properties with
           matching values are enumerated. That is, filters are ORed.
    
           The **sd_device_enumerator_add_match_property_required()** function
           adds a filter to the _enumerator_ so that only devices with the
           specified _property_ equals to the _value_ are enumerated. This
           function is similar to **sd_device_enumerator_add_match_property()** ,
           but when this is called multiple times, devices that have _all_
           specified properties with matching values are enumerated. That is,
           filters are ANDed.
    
           The **sd_device_enumerator_add_match_subsystem()** function adds a
           filter to the _enumerator_ so that all devices in the specified
           _subsystem_ , when _match_ is **true**. When _match_ is **false** , then all
           devices except those in the specified _subsystem_ are enumerated.
           When called multiple times, positive filters are ORed, and
           negative ones are ANDed.
    
           The **sd_device_enumerator_add_match_sysattr()** function adds a
           filter on the sysfs attribute _sysattr_ matching _value_.  _value_ can
           be a glob pattern. If _value_ is **NULL** , devices that either have (if
           _match_ is **true**) or do not have (if _match_ is **false**) the specified
           _sysattr_ are included, regardless of its value. That is, **NULL** is
           mostly equivalent to "*". When this function is called multiple
           times, only devices that match all specified _sysattr_ filters are
           enumerated. That is, these filters are ANDed.
    
           The **sd_device_enumerator_add_match_sysname()** function adds a
           filter so that only devices whose sysname equals to _sysname_ are
           enumerated.  _sysname_ can be a glob pattern. When called multiple
           times, filters are ORed.
    
           The **sd_device_enumerator_add_nomatch_sysname()** function adds a
           filter so that devices whose sysname equals to _sysname_ are
           excluded from the enumeration. This is useful for excluding
           specific devices from the enumeration process. When called
           multiple times, features are ANDed.
    
           The **sd_device_enumerator_add_match_tag()** function adds a filter so
           that only devices tagged with _tag_ are enumerated. When called
           multiple times, filters are ORed.
    
           The **sd_device_enumerator_allow_uninitialized()** function allows
           devices that have not yet been initialized by udev to be included
           in the enumeration.
    
           The **sd_device_enumerator_add_all_parents()** function enumerates all
           parent devices of the matching devices. This is useful for cases
           where you want to include all parent devices in the enumeration,
           such as when you are interested in the entire device tree leading
           up to a specific device.
    


## RETURN VALUE



           All functions return **0** or a positive integer on success, or a
           negative errno-style error code on failure.
    
       **Errors**
           Returned errors may indicate the following problems:
    
           **-ENOMEM**
               Memory allocation failed.
    
           **-EINVAL**
               One of the arguments is invalid.
    


## EXAMPLES



           **Example 1. Detect Removable USB Devices (Using Match and Exclude)**
    
               /* SPDX-License-Identifier: MIT-0 */
    
               #include <stdbool.h>
               #include <stdio.h>
               #include <systemd/sd-device.h>
    
               int main(void) {
                   __attribute__((cleanup(sd_device_enumerator_unrefp))) sd_device_enumerator *enumerator = NULL;
                   sd_device *device;
                   int r;
    
                   /* Create a new device enumerator */
                   r = sd_device_enumerator_new(&enumerator);
                   if (r < 0) {
                       fprintf(stderr, "Failed to create device enumerator: %s\n", strerror(-r));
                       return 1;
                   }
    
                   /* Include only devices from the "usb" subsystem */
                   r = sd_device_enumerator_add_match_subsystem(enumerator, "usb", true);
                   if (r < 0) {
                       fprintf(stderr, "Failed to add subsystem match: %s\n", strerror(-r));
                       return 1;
                   }
    
                   /*
                    * Exclude devices where the "removable" sysattr is "0"
                    * These are typically non-removable devices like built-in USB interfaces
                    */
                   r = sd_device_enumerator_add_match_sysattr(enumerator, "removable", "0", false);
                   if (r < 0) {
                       fprintf(stderr, "Failed to add sysattr match: %s\n", strerror(-r));
                       return 1;
                   }
    
                   /* Begin enumerating matching devices */
                   for (device = sd_device_enumerator_get_device_first(enumerator);
                        device;
                        device = sd_device_enumerator_get_device_next(enumerator)) {
                       const char *syspath;
    
                       /* Get syspath for the device */
                       if (sd_device_get_syspath(device, &syspath) >= 0)
                           printf("Removable USB device found: %s\n", syspath);
                   }
                   return 0;
               }
    


* * *