This will make running tests a lot simpler.
static int namedev_init_config(void)
{
- char filename[255];
char line[255];
char *temp;
char *temp2;
int retval = 0;
struct config_device dev;
- strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_FILE);
- dbg("opening %s to read as permissions config", filename);
- fd = fopen(filename, "r");
+ dbg("opening %s to read as permissions config", udev_config_filename);
+ fd = fopen(udev_config_filename, "r");
if (fd == NULL) {
- dbg("Can't open %s", filename);
+ dbg("Can't open %s", udev_config_filename);
return -ENODEV;
}
static int namedev_init_permissions(void)
{
- char filename[255];
char line[255];
char *temp;
char *temp2;
int retval = 0;
struct config_device dev;
- strcpy(filename, UDEV_CONFIG_DIR NAMEDEV_CONFIG_PERMISSION_FILE);
- dbg("opening %s to read as permissions config", filename);
- fd = fopen(filename, "r");
+ dbg("opening %s to read as permissions config", udev_config_permission_filename);
+ fd = fopen(udev_config_permission_filename, "r");
if (fd == NULL) {
- dbg("Can't open %s", filename);
+ dbg("Can't open %s", udev_config_permission_filename);
return -ENODEV;
}
struct sysfs_class_device;
-/* namedev config files */
#define COMMENT_CHARACTER '#'
-#define NAMEDEV_CONFIG_PERMISSION_FILE "namedev.permissions"
-#define NAMEDEV_CONFIG_FILE "namedev.config"
enum config_type {
KERNEL_NAME = 0, /* must be 0 to let memset() default to this value */
#include "udevdb.h"
#include "libsysfs/libsysfs.h"
-static char sysfs_path[SYSFS_PATH_MAX];
-
/*
* Right now the major/minor of a device is stored in a file called
* "dev" in sysfs.
char filename[255];
int retval = 0;
- strncpy(filename, UDEV_ROOT, sizeof(filename));
+ strncpy(filename, udev_root, sizeof(filename));
strncat(filename, dev->name, sizeof(filename));
switch (dev->type) {
else
dev.type = 'c';
- retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
- dbg("sysfs_path = %s", sysfs_path);
- if (retval) {
- dbg("sysfs_get_mnt_path failed");
- goto exit;
- }
-
retval = sleep_for_dev(path);
if (retval)
goto exit;
{
char filename[255];
- strncpy(filename, UDEV_ROOT, sizeof(filename));
+ strncpy(filename, udev_root, sizeof(filename));
strncat(filename, name, sizeof(filename));
dbg("unlinking %s", filename);
#include "udevdb.h"
#include "libsysfs/libsysfs.h"
+/* global variables */
+char **main_argv;
+char **main_envp;
+
+char sysfs_path[SYSFS_PATH_MAX];
+char *udev_config_dir;
+char *udev_root;
+char udev_db_filename[PATH_MAX+NAME_MAX];
+char udev_config_permission_filename[PATH_MAX+NAME_MAX];
+char udev_config_filename[PATH_MAX+NAME_MAX];
+
-static char *get_action(void)
+static inline char *get_action(void)
{
char *action;
return action;
}
+static inline char *get_devpath(void)
+{
+ char *devpath;
+
+ devpath = getenv("DEVPATH");
+ return devpath;
+}
-static char *get_device(void)
+static inline char *get_seqnum(void)
{
- char *device;
+ char *seqnum;
- device = getenv("DEVPATH");
- return device;
+ seqnum = getenv("SEQNUM");
+ return seqnum;
}
-char **main_argv;
-char **main_envp;
+static void get_dirs(void)
+{
+ char *udev_test;
+ char *temp;
+ int retval;
+
+ udev_test = getenv("UDEV_TEST");
+ if (udev_test == NULL) {
+ /* normal operation, use the compiled in defaults */
+ udev_config_dir = UDEV_CONFIG_DIR;
+ udev_root = UDEV_ROOT;
+ retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
+ dbg("sysfs_path = %s", sysfs_path);
+ if (retval)
+ dbg("sysfs_get_mnt_path failed");
+
+ } else {
+ /* hm testing is happening, use the specified values */
+ temp = getenv("UDEV_SYSFS_PATH");
+ strncpy(sysfs_path, temp, sizeof(sysfs_path));
+ udev_config_dir = getenv("UDEV_CONFIG_DIR");
+ udev_root = getenv("UDEV_ROOT");
+ }
+
+ strncpy(udev_db_filename, udev_config_dir, sizeof(udev_db_filename));
+ strncat(udev_db_filename, UDEV_DB, sizeof(udev_db_filename));
+
+ strncpy(udev_config_filename, udev_config_dir, sizeof(udev_config_filename));
+ strncat(udev_config_filename, NAMEDEV_CONFIG_FILE, sizeof(udev_config_filename));
+
+ strncpy(udev_config_permission_filename, udev_config_dir, sizeof(udev_config_permission_filename));
+ strncat(udev_config_permission_filename, NAMEDEV_CONFIG_PERMISSION_FILE, sizeof(udev_config_permission_filename));
+}
int main(int argc, char **argv, char **envp)
{
char *action;
- char *device;
+ char *devpath;
char *subsystem;
int retval = -EINVAL;
subsystem = argv[1];
- device = get_device();
- if (!device) {
- dbg ("no device?");
+ devpath = get_devpath();
+ if (!devpath) {
+ dbg ("no devpath?");
goto exit;
}
- dbg("looking at %s", device);
+ dbg("looking at %s", devpath);
/* we only care about class devices and block stuff */
- if (!strstr(device, "class") &&
- !strstr(device, "block")) {
+ if (!strstr(devpath, "class") &&
+ !strstr(devpath, "block")) {
dbg("not block or class");
goto exit;
}
}
/* initialize udev database */
+ get_dirs();
retval = udevdb_init(UDEVDB_DEFAULT);
if (retval != 0) {
dbg("Unable to initialize database.");
namedev_init();
if (strcmp(action, "add") == 0)
- retval = udev_add_device(device, argv[1]);
+ retval = udev_add_device(devpath, subsystem);
else if (strcmp(action, "remove") == 0)
- retval = udev_remove_device(device, argv[1]);
+ retval = udev_remove_device(devpath, subsystem);
else {
dbg("Unknown action: %s", action);
#define UDEV_H
#include "libsysfs/libsysfs.h"
+#include <limits.h>
#ifdef DEBUG
#include <syslog.h>
__attribute__ ((format (printf, 2, 3)));
-/* Lots of constants that should be in a config file sometime */
+/* filenames for the config and database files */
+#define UDEV_DB "udevdb.tdb"
+#define NAMEDEV_CONFIG_PERMISSION_FILE "namedev.permissions"
+#define NAMEDEV_CONFIG_FILE "namedev.config"
#define NAME_SIZE 100
#define OWNER_SIZE 30
extern char **main_argv;
extern char **main_envp;
+extern char sysfs_path[SYSFS_PATH_MAX];
+extern char *udev_config_dir;
+extern char *udev_root;
+extern char udev_db_filename[PATH_MAX+NAME_MAX];
+extern char udev_config_permission_filename[PATH_MAX+NAME_MAX];
+extern char udev_config_filename[PATH_MAX+NAME_MAX];
+
#endif
if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
return -EINVAL;
- udevdb = tdb_open(UDEV_CONFIG_DIR UDEV_DB, 0, init_flag, O_RDWR | O_CREAT, 0644);
+ udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
if (udevdb == NULL) {
if (init_flag == UDEVDB_INTERNAL)
dbg("Unable to initialize in-memory database");
else
- dbg("Unable to initialize database at %s", UDEV_CONFIG_DIR UDEV_DB);
+ dbg("Unable to initialize database at %s", udev_db_filename);
return -EINVAL;
}
return 0;
#ifndef _UDEVDB_H_
#define _UDEVDB_H_
-#define UDEV_DB "udevdb.tdb"
-
/* Udevdb initialization flags */
#define UDEVDB_DEFAULT 0 /* Defaults database to use file */
#define UDEVDB_INTERNAL 1 /* Don't store db on disk, use in memory */