test-libudev.c revision 1298001ec5e320f9f9b6a9b925c8939b2579396d
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdio.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdarg.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdlib.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include <getopt.h>
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include <syslog.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <fcntl.h>
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include <sys/epoll.h>
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "libudev.h"
23c80348e656a4e6fd9ba8f17523a65b6fa349a0Kay Sievers#include "util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void log_fn(struct udev *udev,
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering int priority, const char *file, int line, const char *fn,
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering const char *format, va_list args)
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering{
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("test-libudev: %s %s:%d ", fn, file, line);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering vprintf(format, args);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering}
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poetteringstatic void print_device(struct udev_device *device)
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering{
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering const char *str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dev_t devnum;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering int count;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_list_entry *list_entry;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("*** device: %p ***\n", device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_action(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("action: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_syspath(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("syspath: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_sysname(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("sysname: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_sysnum(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("sysnum: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_devpath(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("devpath: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_subsystem(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("subsystem: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_devtype(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("devtype: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_driver(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("driver: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_devnode(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("devname: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering devnum = udev_device_get_devnum(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (major(devnum) > 0)
5bcd08db289cd02aad8a89b37b2a46244a7bd473Lennart Poettering printf("devnum: %u:%u\n", major(devnum), minor(devnum));
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering count = 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("link: '%s'\n", udev_list_entry_get_name(list_entry));
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering count++;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (count > 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("found %i links\n", count);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering count = 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device)) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("property: '%s=%s'\n",
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_get_name(list_entry),
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_list_entry_get_value(list_entry));
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering count++;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (count > 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("found %i properties\n", count);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_property_value(device, "MAJOR");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("MAJOR: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering str = udev_device_get_sysattr_value(device, "dev");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (str != NULL)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("attr{dev}: '%s'\n", str);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("\n");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int test_device(struct udev *udev, const char *syspath)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering{
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_device *device;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("looking at device: %s\n", syspath);
5bcd08db289cd02aad8a89b37b2a46244a7bd473Lennart Poettering device = udev_device_new_from_syspath(udev, syspath);
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering if (device == NULL) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering printf("no device found\n");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering print_device(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering udev_device_unref(device);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return 0;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int test_device_parents(struct udev *udev, const char *syspath)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering{
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering struct udev_device *device;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct udev_device *device_parent;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking at device: %s\n", syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_syspath(udev, syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (device == NULL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking at parents\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device_parent = device;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering do {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering print_device(device_parent);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering device_parent = udev_device_get_parent(device_parent);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering } while (device_parent != NULL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking at parents again\n");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering device_parent = device;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering do {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering print_device(device_parent);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device_parent = udev_device_get_parent(device_parent);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering } while (device_parent != NULL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(device);
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return 0;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int test_device_devnum(struct udev *udev)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering{
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering dev_t devnum = makedev(1, 3);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering struct udev_device *device;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering device = udev_device_new_from_devnum(udev, 'c', devnum);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (device == NULL)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return -1;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering print_device(device);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering udev_device_unref(device);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return 0;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering}
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int test_device_subsys_name(struct udev *udev)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering{
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering struct udev_device *device;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering printf("looking up device: 'block':'sda'\n");
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "block", "sda");
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (device == NULL)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return -1;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering print_device(device);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering udev_device_unref(device);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("looking up device: 'subsystem':'pci'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (device == NULL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -1;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering print_device(device);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(device);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("looking up device: 'drivers':'scsi:sd'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "drivers", "scsi:sd");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (device == NULL)
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering return -1;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering print_device(device);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(device);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("looking up device: 'module':'printk'\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_subsystem_sysname(udev, "module", "printk");
06820eafdbc3dd89cb1f7563564c7d91426709caLennart Poettering if (device == NULL)
06820eafdbc3dd89cb1f7563564c7d91426709caLennart Poettering return -1;
06820eafdbc3dd89cb1f7563564c7d91426709caLennart Poettering print_device(device);
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering udev_device_unref(device);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return 0;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering}
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poetteringstatic int test_enumerate_print_list(struct udev_enumerate *enumerate)
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering{
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering struct udev_list_entry *list_entry;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering int count = 0;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering struct udev_device *device;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_list_entry_get_name(list_entry));
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (device != NULL) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("device: '%s' (%s)\n",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek udev_device_get_syspath(device),
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_get_subsystem(device));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(device);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering count++;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering printf("found %i devices\n\n", count);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return count;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int test_monitor(struct udev *udev)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering{
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct udev_monitor *udev_monitor = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int fd_ep;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int fd_udev = -1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct epoll_event ep_udev, ep_stdin;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (fd_ep < 0) {
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering printf("error creating epoll fd: %m\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor == NULL) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("no socket\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_udev = udev_monitor_get_fd(udev_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", NULL) < 0 ||
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "tty", NULL) < 0 ||
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device") < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("filter failed\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor_enable_receiving(udev_monitor) < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("bind failed\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering memset(&ep_udev, 0, sizeof(struct epoll_event));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering ep_udev.events = EPOLLIN;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering ep_udev.data.fd = fd_udev;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("fail to add fd to epoll: %m\n");
9030ca462bd13cd6536299814e4a71d5c5e85be9Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering memset(&ep_stdin, 0, sizeof(struct epoll_event));
8aec412ff697bc14995746953912ca6fdf2c9ba8Lennart Poettering ep_stdin.events = EPOLLIN;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering ep_stdin.data.fd = STDIN_FILENO;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, STDIN_FILENO, &ep_stdin) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("fail to add fd to epoll: %m\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering for (;;) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int fdcount;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct epoll_event ev[4];
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct udev_device *device;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering int i;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("waiting for events from udev, press ENTER to exit\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("epoll fd count: %i\n", fdcount);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
7f0d207d2c816e0a8cb2742b0a789911f7c99356Lennart Poettering for (i = 0; i < fdcount; i++) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering device = udev_monitor_receive_device(udev_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (device == NULL) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("no device from socket\n");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering print_device(device);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_device_unref(device);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (ev[i].data.fd == STDIN_FILENO && ev[i].events & EPOLLIN) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("exiting loop\n");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (fd_ep >= 0)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering close(fd_ep);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_monitor_unref(udev_monitor);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return 0;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering}
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poetteringstatic int test_queue(struct udev *udev)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering{
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering struct udev_queue *udev_queue;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering unsigned long long int seqnum;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering struct udev_list_entry *list_entry;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_queue = udev_queue_new(udev);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (udev_queue == NULL)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return -1;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering seqnum = udev_queue_get_kernel_seqnum(udev_queue);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum kernel: %llu\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering seqnum = udev_queue_get_udev_seqnum(udev_queue);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum udev : %llu\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_queue_get_queue_is_empty(udev_queue))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("queue is empty\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("get queue list\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("get queue list again\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("queued: '%s' [%s]\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering list_entry = udev_queue_get_queued_list_entry(udev_queue);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (list_entry != NULL) {
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering printf("event [%llu] is queued\n", seqnum);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering seqnum = strtoull(udev_list_entry_get_value(list_entry), NULL, 10);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering if (udev_queue_get_seqnum_is_finished(udev_queue, seqnum))
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering printf("event [%llu] is not finished\n", seqnum);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering else
554604b3073467af75dc94fac9e2343148603289Lennart Poettering printf("event [%llu] is finished\n", seqnum);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("\n");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_queue_unref(udev_queue);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return 0;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poetteringstatic int test_enumerate(struct udev *udev, const char *subsystem)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_enumerate *udev_enumerate;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_enumerate == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, subsystem);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_enumerate_print_list(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_unref(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("enumerate 'net' + duplicated scan + null + zero\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_enumerate == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "net");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_scan_devices(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/null");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_add_syspath(udev_enumerate, "/sys/class/mem/zero");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering test_enumerate_print_list(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_unref(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering printf("enumerate 'block'\n");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate = udev_enumerate_new(udev);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (udev_enumerate == NULL)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering return -1;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate,"block");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_is_initialized(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_unref(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering printf("enumerate 'not block'\n");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate = udev_enumerate_new(udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (udev_enumerate == NULL)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return -1;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_unref(udev_enumerate);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("enumerate 'pci, mem, vc'\n");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (udev_enumerate == NULL)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return -1;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "pci");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "mem");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_add_match_subsystem(udev_enumerate, "vc");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering test_enumerate_print_list(udev_enumerate);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate_unref(udev_enumerate);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering printf("enumerate 'subsystem'\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_enumerate == NULL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return -1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_enumerate_scan_subsystems(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_enumerate_print_list(udev_enumerate);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_enumerate_unref(udev_enumerate);
a3e7f417d72ba3251fd6b3a228a2721a4b725a03Zbigniew Jędrzejewski-Szmek
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering printf("enumerate 'property IF_FS_*=filesystem'\n");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_enumerate = udev_enumerate_new(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_enumerate == NULL)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return -1;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_enumerate_add_match_property(udev_enumerate, "ID_FS*", "filesystem");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_enumerate_scan_devices(udev_enumerate);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering test_enumerate_print_list(udev_enumerate);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_enumerate_unref(udev_enumerate);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return 0;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering}
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poetteringstatic int test_hwdb(struct udev *udev, const char *modalias) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct udev_hwdb * hwdb;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct udev_list_entry *entry;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering hwdb = udev_hwdb_new(udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0))
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("'%s'='%s'\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("\n");
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering hwdb = udev_hwdb_unref(hwdb);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return 0;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poetteringint main(int argc, char *argv[])
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering{
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct udev *udev = NULL;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering static const struct option options[] = {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "syspath", required_argument, NULL, 'p' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "subsystem", required_argument, NULL, 's' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "debug", no_argument, NULL, 'd' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "help", no_argument, NULL, 'h' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering { "version", no_argument, NULL, 'V' },
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering {}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering };
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering const char *syspath = "/devices/virtual/mem/null";
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering const char *subsystem = NULL;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering char path[1024];
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev = udev_new();
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering printf("context: %p\n", udev);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (udev == NULL) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering printf("no context\n");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_set_log_fn(udev, log_fn);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering printf("set log: %p\n", log_fn);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering for (;;) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering int option;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering option = getopt_long(argc, argv, "+p:s:dhV", options, NULL);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (option == -1)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering break;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (option) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case 'p':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering syspath = optarg;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 's':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsystem = optarg;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'd':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_get_log_priority(udev) < LOG_INFO)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_set_log_priority(udev, LOG_INFO);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 'h':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering printf("--debug --syspath= --subsystem= --help\n");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering case 'V':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s\n", VERSION);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering default:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* add sys path if needed */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!startswith(syspath, "/sys")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering snprintf(path, sizeof(path), "/sys/%s", syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering syspath = path;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering test_device(udev, syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_device_devnum(udev);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering test_device_subsys_name(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_device_parents(udev, syspath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_enumerate(udev, subsystem);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_queue(udev);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering test_hwdb(udev, "usb:v0D50p0011*");
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering test_monitor(udev);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringout:
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering udev_unref(udev);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return 0;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering