udevtest.c revision e5e322bc627a07d29a07e08f7c96bd644a3ae057
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering * udevtest.c
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers *
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * Userspace devfs
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers *
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers *
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * This program is free software; you can redistribute it and/or modify it
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * under the terms of the GNU General Public License as published by the
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * Free Software Foundation version 2 of the License.
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers *
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * This program is distributed in the hope that it will be useful, but
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * WITHOUT ANY WARRANTY; without even the implied warranty of
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * General Public License for more details.
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers *
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * You should have received a copy of the GNU General Public License along
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * with this program; if not, write to the Free Software Foundation, Inc.,
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers * 675 Mass Ave, Cambridge, MA 02139, USA.
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering *
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
69beda1f75070b36d0562e4050cd567bf2da5a87Kay Sievers#include <stdlib.h>
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include <string.h>
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include <stdio.h>
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include <errno.h>
c9679c652b3c31f2510e8805d81630680ebc7e95Lennart Poettering#include <ctype.h>
c9679c652b3c31f2510e8805d81630680ebc7e95Lennart Poettering#include <signal.h>
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "libsysfs/sysfs/libsysfs.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "udev.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "udev_sysfs.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "udev_utils.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "udev_version.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "udev_rules.h"
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#include "logging.h"
69beda1f75070b36d0562e4050cd567bf2da5a87Kay Sievers
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#ifdef USE_LOG
c9679c652b3c31f2510e8805d81630680ebc7e95Lennart Poetteringvoid log_message (int level, const char *format, ...)
c9679c652b3c31f2510e8805d81630680ebc7e95Lennart Poettering{
c9679c652b3c31f2510e8805d81630680ebc7e95Lennart Poettering va_list args;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
69beda1f75070b36d0562e4050cd567bf2da5a87Kay Sievers va_start(args, format);
69beda1f75070b36d0562e4050cd567bf2da5a87Kay Sievers vprintf(format, args);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering va_end(args);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (format[strlen(format)-1] != '\n')
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering printf("\n");
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering}
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering#endif
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[], char *envp[])
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering{
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering struct sysfs_class_device *class_dev;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering char *devpath;
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek char path[PATH_SIZE];
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering char temp[PATH_SIZE];
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering struct udevice udev;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering char *subsystem = NULL;
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("version %s", UDEV_VERSION);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (argc < 2 || argc > 3) {
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("Usage: udevtest <devpath> [subsystem]");
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering return 1;
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek }
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek /* initialize our configuration */
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek udev_init_config();
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* remove sysfs_path if given */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering devpath = &argv[1][strlen(sysfs_path)] ;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering else
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (argv[1][0] != '/') {
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* prepend '/' if missing */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering snprintf(temp, sizeof(temp), "/%s", argv[1]);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering temp[sizeof(temp)-1] = '\0';
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering devpath = temp;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering } else
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering devpath = argv[1];
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("looking at '%s'", devpath);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* initialize the naming deamon */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering udev_rules_init();
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (argc == 3)
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering subsystem = argv[2];
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* fill in values and test_run flag*/
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering udev_init_device(&udev, devpath, subsystem);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* skip subsystems without "dev", but handle net devices */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (udev.type != DEV_NET && subsystem_expect_no_dev(udev.subsystem)) {
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("don't care about '%s' devices", udev.subsystem);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering return 2;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering }
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* open the device */
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering path[sizeof(path)-1] = '\0';
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering class_dev = sysfs_open_class_device_path(path);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering if (class_dev == NULL) {
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("sysfs_open_class_device_path failed");
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering return 1;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering }
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering info("opened class_dev->name='%s'", class_dev->name);
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering /* simulate node creation with test flag */
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen udev.test_run = 1;
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen udev_add_device(&udev, class_dev);
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen sysfs_close_class_device(class_dev);
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen
68dd0956ef9d607e6ff9aea15883a2c290a33c2aTom Gundersen return 0;
6936cd8926b6935364874b3701e86fe823e8c4ceLennart Poettering}
499b604b21c02ee64c8590a76d7900d64d7a5cb7Zbigniew Jędrzejewski-Szmek