1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

udevadm: test - remove --force option

Since a while we change the database with a "test" run, but do not update
the node and symlinks. We need to "force" all the time, to keep things
in sync.
This commit is contained in:
Kay Sievers 2009-01-15 20:20:16 +01:00
parent 64b333fdcc
commit ec2dd02e2a
6 changed files with 42 additions and 74 deletions

View File

@ -472,8 +472,6 @@ static int rename_netif(struct udev_event *event)
info(event->udev, "changing net interface name from '%s' to '%s'\n",
udev_device_get_sysname(dev), event->name);
if (event->test)
return 0;
sk = socket(PF_INET, SOCK_DGRAM, 0);
if (sk < 0) {
@ -593,11 +591,11 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
/* update database, create node and symlinks */
udev_device_update_db(dev);
err = udev_node_add(dev, event->mode, event->uid, event->gid, event->test);
err = udev_node_add(dev, event->mode, event->uid, event->gid);
/* remove/update possible left-over symlinks from old database entry */
if (dev_old != NULL) {
udev_node_update_old_links(dev, dev_old, event->test);
udev_node_update_old_links(dev, dev_old);
udev_device_unref(dev_old);
}
goto exit;
@ -673,7 +671,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
goto exit;
}
err = udev_node_remove(dev, event->test);
err = udev_node_remove(dev);
goto exit;
}

View File

@ -32,7 +32,7 @@
#define TMP_FILE_EXT ".udev-tmp"
/* reverse mapping from the device file name to the devpath */
static int name_index(struct udev *udev, const char *devpath, const char *name, int add, int test)
static int name_index(struct udev *udev, const char *devpath, const char *name, int add)
{
char device[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE * 2];
@ -268,7 +268,7 @@ out:
return count;
}
static int update_link(struct udev_device *dev, const char *slink, int test)
static int update_link(struct udev_device *dev, const char *slink)
{
struct udev *udev = udev_device_get_udev(dev);
struct udev_list_node dev_list;
@ -288,10 +288,8 @@ static int update_link(struct udev_device *dev, const char *slink, int test)
/* if we don't have a reference, delete it */
if (count <= 0) {
info(udev, "no reference left, remove '%s'\n", slink);
if (!test) {
unlink(slink);
util_delete_path(udev, slink);
}
unlink(slink);
util_delete_path(udev, slink);
goto out;
}
@ -351,15 +349,13 @@ static int update_link(struct udev_device *dev, const char *slink, int test)
/* create symlink to the target with the highest priority */
info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority);
if (!test) {
util_create_path(udev, slink);
node_symlink(udev, target, slink);
}
util_create_path(udev, slink);
node_symlink(udev, target, slink);
out:
return rc;
}
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test)
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old)
{
struct udev *udev = udev_device_get_udev(dev);
struct udev_list_entry *list_entry;
@ -389,8 +385,8 @@ void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev
continue;
info(udev, "update old symlink '%s' no longer belonging to '%s'\n", name, udev_device_get_devpath(dev));
name_index(udev, udev_device_get_devpath(dev), name, 0, test);
update_link(dev, name, test);
name_index(udev, udev_device_get_devpath(dev), name, 0);
update_link(dev, name);
}
/*
@ -402,11 +398,11 @@ void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev
const char *devnode = udev_device_get_devnode(dev);
if (devnode != NULL && strcmp(devnode_old, devnode) != 0)
update_link(dev, devnode_old, test);
update_link(dev, devnode_old);
}
}
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid, int test)
int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid)
{
struct udev *udev = udev_device_get_udev(dev);
int i;
@ -420,44 +416,41 @@ int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid, in
mode, uid, gid);
util_create_path(udev, udev_device_get_devnode(dev));
if (!test)
if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
err = -1;
goto exit;
}
if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
err = -1;
goto exit;
}
/* create all_partitions if requested */
num = udev_device_get_num_fake_partitions(dev);
if (num > 0) {
info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
if (!test) {
for (i = 1; i <= num; i++) {
char partitionname[UTIL_PATH_SIZE];
dev_t part_devnum;
for (i = 1; i <= num; i++) {
char partitionname[UTIL_PATH_SIZE];
dev_t part_devnum;
snprintf(partitionname, sizeof(partitionname), "%s%d",
udev_device_get_devnode(dev), i);
partitionname[sizeof(partitionname)-1] = '\0';
part_devnum = makedev(major(udev_device_get_devnum(dev)),
minor(udev_device_get_devnum(dev)) + i);
udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
}
snprintf(partitionname, sizeof(partitionname), "%s%d",
udev_device_get_devnode(dev), i);
partitionname[sizeof(partitionname)-1] = '\0';
part_devnum = makedev(major(udev_device_get_devnum(dev)),
minor(udev_device_get_devnum(dev)) + i);
udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
}
}
/* add node to name index */
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1, test);
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1);
/* create/update symlinks, add symlinks to name index */
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1, test);
update_link(dev, udev_list_entry_get_name(list_entry), test);
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1);
update_link(dev, udev_list_entry_get_name(list_entry));
}
exit:
return err;
}
extern int udev_node_remove(struct udev_device *dev, int test)
extern int udev_node_remove(struct udev_device *dev)
{
struct udev *udev = udev_device_get_udev(dev);
struct udev_list_entry *list_entry;
@ -468,12 +461,12 @@ extern int udev_node_remove(struct udev_device *dev, int test)
int num;
/* remove node from name index */
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0, test);
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0);
/* remove,update symlinks, remove symlinks from name index */
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0, test);
update_link(dev, udev_list_entry_get_name(list_entry), test);
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0);
update_link(dev, udev_list_entry_get_name(list_entry));
}
devnode = udev_device_get_devnode(dev);
@ -489,8 +482,7 @@ extern int udev_node_remove(struct udev_device *dev, int test)
}
info(udev, "removing device node '%s'\n", devnode);
if (!test)
err = util_unlink_secure(udev, devnode);
err = util_unlink_secure(udev, devnode);
if (err)
return err;
@ -504,8 +496,7 @@ extern int udev_node_remove(struct udev_device *dev, int test)
for (i = 1; i <= num; i++) {
snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
partitionname[sizeof(partitionname)-1] = '\0';
if (!test)
util_unlink_secure(udev, partitionname);
util_unlink_secure(udev, partitionname);
}
}
util_delete_path(udev, devnode);

View File

@ -2504,9 +2504,8 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
rule->rule.filename_line);
f = fopen(attr, "w");
if (f != NULL) {
if (!event->test)
if (fprintf(f, "%s", value) <= 0)
err(event->udev, "error writing ATTR{%s}: %m\n", attr);
if (fprintf(f, "%s", value) <= 0)
err(event->udev, "error writing ATTR{%s}: %m\n", attr);
fclose(f);
} else {
err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);

View File

@ -76,7 +76,6 @@ struct udev_event {
unsigned int run_final:1;
unsigned int ignore_device:1;
unsigned int trace:1;
unsigned int test:1;
};
/* udev-rules.c */
@ -96,9 +95,9 @@ extern int udev_event_apply_subsys_kernel(struct udev_event *event, const char *
/* udev-node.c */
extern int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid);
extern int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid, int test);
extern int udev_node_remove(struct udev_device *dev, int test);
extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test);
extern int udev_node_add(struct udev_device *dev, mode_t mode, uid_t uid, gid_t gid);
extern int udev_node_remove(struct udev_device *dev);
extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old);
/* udev-util.c */
extern int util_create_path(struct udev *udev, const char *path);

View File

@ -33,7 +33,6 @@
int udevadm_test(struct udev *udev, int argc, char *argv[])
{
char filename[UTIL_PATH_SIZE];
int force = 0;
const char *action = "add";
const char *syspath = NULL;
struct udev_event *event;
@ -44,7 +43,6 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
static const struct option options[] = {
{ "action", required_argument, NULL, 'a' },
{ "force", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{}
};
@ -63,13 +61,9 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
case 'a':
action = optarg;
break;
case 'f':
force = 1;
break;
case 'h':
printf("Usage: udevadm test OPTIONS <syspath>\n"
" --action=<string> set action string\n"
" --force don't skip node/link creation\n"
" --help print this help text\n\n");
exit(0);
default:
@ -116,11 +110,6 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
udev_device_set_action(dev, action);
event = udev_event_new(dev);
/* simulate node creation with test flag */
if (!force)
event->test = 1;
err = udev_event_execute_rules(event, rules);
if (udev_device_get_event_timeout(dev) >= 0)

View File

@ -326,8 +326,7 @@
</refsect2>
<refsect2><title>udevadm test <optional>options</optional> <replaceable>devpath</replaceable></title>
<para>Simulate a udev event run for the given device, and print out debug
output. Unless forced to, no device node or symlink will be created.</para>
<para>Simulate a udev event run for the given device, and print debug output.</para>
<variablelist>
<varlistentry>
<term><option>--action=<replaceable>string</replaceable></option></term>
@ -341,13 +340,6 @@
<para>The subsystem string.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--force</option></term>
<listitem>
<para>Force the creation of a device node or symlink. Usually the test run
prints only debug output.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--help</option></term>
<listitem>