1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-27 07:22:31 +03:00
systemd/udev/lib/test-libudev.c

318 lines
7.6 KiB
C
Raw Normal View History

/*
* test-libudev
*
* Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdarg.h>
2008-08-29 01:05:01 +04:00
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <fcntl.h>
2008-08-29 01:05:01 +04:00
#include <sys/select.h>
#include "libudev.h"
static void log_fn(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args)
{
printf("test-libudev: %s %s:%d ", fn, file, line);
vprintf(format, args);
}
2008-08-29 01:05:01 +04:00
static void print_device(struct udev_device *device)
{
const char *str;
2008-09-26 01:26:22 +04:00
dev_t devnum;
2008-08-29 01:05:01 +04:00
int count;
2008-09-28 03:34:55 +04:00
struct udev_list_entry *list_entry;
2008-08-29 01:05:01 +04:00
printf("*** device: %p ***\n", device);
str = udev_device_get_action(device);
2008-09-28 03:34:55 +04:00
if (str != NULL)
printf("action: '%s'\n", str);
str = udev_device_get_syspath(device);
printf("syspath: '%s'\n", str);
2008-09-28 03:34:55 +04:00
2008-08-29 01:05:01 +04:00
str = udev_device_get_devpath(device);
printf("devpath: '%s'\n", str);
2008-09-28 03:34:55 +04:00
2008-08-29 01:05:01 +04:00
str = udev_device_get_subsystem(device);
2008-09-28 03:34:55 +04:00
if (str != NULL)
printf("subsystem: '%s'\n", str);
str = udev_device_get_driver(device);
2008-09-28 03:34:55 +04:00
if (str != NULL)
printf("driver: '%s'\n", str);
str = udev_device_get_devnode(device);
2008-09-28 03:34:55 +04:00
if (str != NULL)
printf("devname: '%s'\n", str);
2008-09-26 01:26:22 +04:00
devnum = udev_device_get_devnum(device);
2008-09-28 03:34:55 +04:00
if (major(devnum) > 0)
printf("devnum: %u:%u\n", major(devnum), minor(devnum));
count = 0;
2008-09-28 03:34:55 +04:00
list_entry = udev_device_get_devlinks_list_entry(device);
while (list_entry != NULL) {
printf("link: '%s'\n", udev_list_entry_get_name(list_entry));
count++;
2008-09-28 03:34:55 +04:00
list_entry = udev_list_entry_get_next(list_entry);
}
2008-09-28 03:34:55 +04:00
if (count > 0)
printf("found %i links\n", count);
count = 0;
2008-09-28 03:34:55 +04:00
list_entry = udev_device_get_properties_list_entry(device);
while (list_entry != NULL) {
printf("property: '%s=%s'\n",
udev_list_entry_get_name(list_entry),
udev_list_entry_get_value(list_entry));
count++;
2008-09-28 03:34:55 +04:00
list_entry = udev_list_entry_get_next(list_entry);
}
2008-09-28 03:34:55 +04:00
if (count > 0)
printf("found %i properties\n", count);
str = udev_device_get_attr_value(device, "dev");
2008-09-28 03:34:55 +04:00
if (str != NULL)
printf("attr{dev}: '%s'\n", str);
2008-08-29 01:05:01 +04:00
printf("\n");
}
static int test_device(struct udev *udev, const char *syspath)
2008-08-29 01:05:01 +04:00
{
struct udev_device *device;
printf("looking at device: %s\n", syspath);
device = udev_device_new_from_syspath(udev, syspath);
2008-08-29 01:05:01 +04:00
if (device == NULL) {
printf("no device\n");
return -1;
}
print_device(device);
udev_device_unref(device);
return 0;
}
static int test_device_parents(struct udev *udev, const char *syspath)
2008-09-11 19:08:12 +04:00
{
struct udev_device *device;
struct udev_device *device_parent;
2008-09-11 19:08:12 +04:00
printf("looking at device: %s\n", syspath);
device = udev_device_new_from_syspath(udev, syspath);
if (device == NULL)
return -1;
printf("looking at parents\n");
device_parent = device;
do {
print_device(device_parent);
device_parent = udev_device_get_parent(device_parent);
} while (device_parent != NULL);
printf("looking at parents again\n");
device_parent = device;
do {
print_device(device_parent);
device_parent = udev_device_get_parent(device_parent);
} while (device_parent != NULL);
udev_device_unref(device);
2008-09-11 19:08:12 +04:00
return 0;
}
static int test_device_devnum(struct udev *udev)
{
dev_t devnum = makedev(1, 3);
struct udev_device *device;
printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
device = udev_device_new_from_devnum(udev, 'c', devnum);
if (device == NULL)
return -1;
print_device(device);
udev_device_unref(device);
return 0;
}
2008-08-29 01:05:01 +04:00
static int test_enumerate(struct udev *udev, const char *subsystem)
{
struct udev_enumerate *enumerate;
2008-09-28 03:34:55 +04:00
struct udev_list_entry *list_entry;
int count = 0;
2008-08-29 01:05:01 +04:00
enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
if (enumerate == NULL)
return -1;
2008-09-28 03:34:55 +04:00
list_entry = udev_enumerate_get_list_entry(enumerate);
while (list_entry != NULL) {
struct udev_device *device;
2008-09-28 03:34:55 +04:00
device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
if (device != NULL) {
printf("device: '%s' (%s) '%s'\n",
udev_device_get_syspath(device),
udev_device_get_subsystem(device),
udev_device_get_sysname(device));
udev_device_unref(device);
count++;
}
2008-09-28 03:34:55 +04:00
list_entry = udev_list_entry_get_next(list_entry);
}
udev_enumerate_unref(enumerate);
2008-08-29 01:05:01 +04:00
printf("found %i devices\n\n", count);
return count;
}
static int test_monitor(struct udev *udev, const char *socket_path)
{
struct udev_monitor *udev_monitor;
fd_set readfds;
int fd;
udev_monitor = udev_monitor_new_from_socket(udev, socket_path);
if (udev_monitor == NULL) {
printf("no socket\n");
return -1;
}
2008-09-08 19:59:00 +04:00
if (udev_monitor_enable_receiving(udev_monitor) < 0) {
printf("bind failed\n");
return -1;
}
2008-08-29 01:05:01 +04:00
fd = udev_monitor_get_fd(udev_monitor);
FD_ZERO(&readfds);
while (1) {
struct udev_device *device;
int fdcount;
FD_SET(STDIN_FILENO, &readfds);
FD_SET(fd, &readfds);
printf("waiting for events on %s, press ENTER to exit\n", socket_path);
fdcount = select(fd+1, &readfds, NULL, NULL, NULL);
printf("select fd count: %i\n", fdcount);
if (FD_ISSET(fd, &readfds)) {
2008-09-08 19:59:00 +04:00
device = udev_monitor_receive_device(udev_monitor);
2008-08-29 01:05:01 +04:00
if (device == NULL) {
printf("no device from socket\n");
continue;
}
print_device(device);
udev_device_unref(device);
}
if (FD_ISSET(STDIN_FILENO, &readfds)) {
printf("exiting loop\n");
break;
}
}
udev_monitor_unref(udev_monitor);
return 0;
}
int main(int argc, char *argv[], char *envp[])
{
struct udev *udev = NULL;
static const struct option options[] = {
{ "syspath", 1, NULL, 'p' },
{ "subsystem", 1, NULL, 's' },
{ "socket", 1, NULL, 'S' },
{ "debug", 0, NULL, 'd' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{}
};
const char *syspath = "/devices/virtual/mem/null";
const char *subsystem = NULL;
2008-08-29 01:05:01 +04:00
const char *socket = "@/org/kernel/udev/monitor";
char path[1024];
2008-08-29 01:05:01 +04:00
const char *str;
udev = udev_new();
printf("context: %p\n", udev);
if (udev == NULL) {
printf("no context\n");
return 1;
}
udev_set_log_fn(udev, log_fn);
printf("set log: %p\n", log_fn);
while (1) {
int option;
option = getopt_long(argc, argv, "+dhV", options, NULL);
if (option == -1)
break;
switch (option) {
case 'p':
syspath = optarg;
break;
case 's':
subsystem = optarg;
break;
case 'S':
socket = optarg;
break;
case 'd':
if (udev_get_log_priority(udev) < LOG_INFO)
udev_set_log_priority(udev, LOG_INFO);
break;
case 'h':
printf("--debug --syspath= --subsystem= --socket= --help\n");
goto out;
case 'V':
printf("%s\n", VERSION);
goto out;
default:
goto out;
}
}
str = udev_get_sys_path(udev);
2008-08-29 01:05:01 +04:00
printf("sys_path: '%s'\n", str);
str = udev_get_dev_path(udev);
2008-08-29 01:05:01 +04:00
printf("dev_path: '%s'\n", str);
/* add sys path if needed */
if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
snprintf(path, sizeof(path), "%s%s", udev_get_sys_path(udev), syspath);
syspath = path;
}
test_device(udev, syspath);
test_device_devnum(udev);
test_device_parents(udev, syspath);
2008-08-29 01:05:01 +04:00
test_enumerate(udev, subsystem);
test_monitor(udev, socket);
out:
udev_unref(udev);
return 0;
}