From: Andrey Borzenkov <arvidjaar@mail.ru>
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] support directory for rules and permissions
Date: Sun, 22 Feb 2004 09:45:21 +0000 [thread overview]
Message-ID: <20040222094521.GC4873@localhost.localdomain> (raw)
[-- Attachment #1: Type: text/plain, Size: 759 bytes --]
On Mon, Feb 16, 2004 at 02:34:33PM -0800, Greg KH wrote:
> On Sat, Jan 17, 2004 at 11:13:00PM +0300, Andrey Borzenkov wrote:
> > Attached patch adds support for
> >
> > - multiple rules files. You can now do
> >
> > udev_rules="file1 dir2 file3 ..."
>
> Care to fix this patch up for the latest udev release?
>
This is new version that I have been runing for some time. It does not
directly support multiple files; rather you can give either file or
directory; directory is scanned, sorted and all files are read in order.
KLIBC case still supports single file only; I do not think anything more
complicated will be needed for initrd.
The only reason for first patch was to ensure config files order and it
is not done automatically.
regards
-andrey
[-- Attachment #2: udev018_config_dir.patch --]
[-- Type: text/plain, Size: 5329 bytes --]
--- udev-018/namedev.c.multi 2004-02-19 21:38:37.000000000 +0300
+++ udev-018/namedev.c 2004-02-21 21:57:19.350887008 +0300
@@ -807,12 +807,12 @@ int namedev_name_device(struct sysfs_cla
if (match_rule(dev, class_dev, udev, sysfs_device) == 0) {
if (dev->name[0] == '\0') {
info("configured rule in '%s' at line %i applied, '%s' is ignored",
- udev_rules_filename, dev->config_line, udev->kernel_name);
+ dev->config_file, dev->config_line, udev->kernel_name);
return -1;
}
info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
- udev_rules_filename, dev->config_line, udev->kernel_name, dev->name);
+ dev->config_file, dev->config_line, udev->kernel_name, dev->name);
strfieldcpy(udev->name, dev->name);
strfieldcpy(udev->symlink, dev->symlink);
goto found;
--- udev-018/namedev.h.multi 2004-02-19 21:38:35.000000000 +0300
+++ udev-018/namedev.h 2004-02-21 21:54:58.000000000 +0300
@@ -71,6 +71,7 @@ struct config_device {
struct sysfs_pair sysfs_pair[MAX_SYSFS_PAIRS];
int partitions;
int config_line;
+ char *config_file;
};
struct perm_device {
--- udev-018/namedev_parse.c.multi 2004-02-19 21:38:35.000000000 +0300
+++ udev-018/namedev_parse.c 2004-02-21 21:47:48.000000000 +0300
@@ -34,6 +34,8 @@
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
+#include <dirent.h>
+#include <sys/stat.h>
#include "udev.h"
#include "logging.h"
@@ -114,7 +116,7 @@ static char *get_key_attribute(char *str
return NULL;
}
-int namedev_init_rules(void)
+static int parse_rules_file(const char *file)
{
char line[255];
int lineno;
@@ -126,15 +128,19 @@ int namedev_init_rules(void)
int program_given = 0;
int retval = 0;
struct config_device dev;
+ char *p;
- fd = fopen(udev_rules_filename, "r");
+ fd = fopen(file, "r");
if (fd != NULL) {
- dbg("reading '%s' as rules file", udev_rules_filename);
+ dbg("reading '%s' as rules file", file);
} else {
- dbg("can't open '%s' as a rules file", udev_rules_filename);
+ dbg("can't open '%s' as a rules file", file);
return -ENODEV;
}
+ /* Yes, this is memory leak. It won't be freed */
+ p = strdup(file);
+
/* loop through the whole file */
lineno = 0;
while (1) {
@@ -256,13 +262,14 @@ int namedev_init_rules(void)
}
dev.config_line = lineno;
+ dev.config_file = p;
retval = add_config_dev(&dev);
if (retval) {
dbg("add_config_dev returned with error %d", retval);
continue;
error:
dbg("%s:%d:%d: parse error, rule skipped",
- udev_rules_filename, lineno, temp - line);
+ file, lineno, temp - line);
}
}
exit:
@@ -270,7 +277,7 @@ exit:
return retval;
}
-int namedev_init_permissions(void)
+static int parse_permissions_file(const char *file)
{
char line[255];
char *temp;
@@ -279,11 +286,11 @@ int namedev_init_permissions(void)
int retval = 0;
struct perm_device dev;
- fd = fopen(udev_permissions_filename, "r");
+ fd = fopen(file, "r");
if (fd != NULL) {
- dbg("reading '%s' as permissions file", udev_permissions_filename);
+ dbg("reading '%s' as permissions file", file);
} else {
- dbg("can't open '%s' as permissions file", udev_permissions_filename);
+ dbg("can't open '%s' as permissions file", file);
return -ENODEV;
}
@@ -350,5 +357,108 @@ int namedev_init_permissions(void)
exit:
fclose(fd);
return retval;
+}
+
+static int ends_with(const char *name, const char *suf)
+{
+ char *p = strstr(name, suf);
+
+ if (!p)
+ return 0;
+
+ if (p[strlen(suf)])
+ return 0;
+
+ return 1;
}
+/*
+ * skip
+ * all hidden files
+ * usual backup files from editor or RPM
+ * non-plain files
+ */
+static int filter(const struct dirent *dirent)
+{
+ char *name = dirent->d_name;
+
+ if (name[0] == '.')
+ return 0;
+
+ if (ends_with(name, ".rpmorig") ||
+ ends_with(name, ".rpmsave") ||
+ ends_with(name, ".rpmnew") ||
+ ends_with(name, "~") ||
+ ends_with(name, ".orig"))
+ return 0;
+
+ return 1;
+}
+
+static int parse_rules(char *file, int (*func)(const char *))
+{
+ struct stat buf;
+ int err = 0;
+
+
+ if (stat(file, &buf) == -1) {
+ err = errno;
+ dbg("parse_rules: can't stat %s", file);
+ return errno;
+ }
+
+ if (S_ISREG(buf.st_mode))
+ return (*func)(file);
+
+#ifndef __KLIBC__
+ if (S_ISDIR(buf.st_mode)) {
+ struct dirent **dirents = 0;
+ int nfiles, i;
+
+ if ((nfiles = scandir(file, &dirents, filter, alphasort)) == -1) {
+ err = errno;
+ dbg("parse_rules: scandir '%s' error", file);
+ return err;
+ }
+
+ for (i = 0; i < nfiles; i++) {
+ char temp[PATH_MAX + NAME_MAX];
+
+ snprintf(temp, sizeof(temp), "%s/%s", file, dirents[i]->d_name);
+ free(dirents[i]);
+
+ if (stat(temp, &buf) == -1) {
+ dbg("parse_rules: cannot stat '%s'", temp);
+ continue;
+ }
+
+ if (!S_ISREG(buf.st_mode)) {
+ dbg("parse_rules: not a regular file '%s'", temp);
+ continue;
+ }
+
+ err = (*func)(temp);
+ if (err)
+ return err;
+ }
+
+ free(dirents);
+
+ return 0;
+ }
+#endif
+
+ dbg("parse_rules: '%s' has unknown file type %o", file, buf.st_mode);
+ return -EINVAL;
+
+}
+
+int namedev_init_rules(void)
+{
+ return parse_rules(udev_rules_filename, parse_rules_file);
+}
+
+int namedev_init_permissions(void)
+{
+ return parse_rules(udev_permissions_filename, parse_permissions_file);
+}
next reply other threads:[~2004-02-22 9:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-02-22 9:45 Andrey Borzenkov [this message]
2004-02-22 10:17 ` [PATCH] support directory for rules and permissions Marco d'Itri
2004-02-22 15:02 ` Andrey Borzenkov
2004-02-22 15:33 ` Marco d'Itri
2004-02-22 18:20 ` Gioele Barabucci
2004-02-22 19:20 ` Marco d'Itri
2004-02-23 0:15 ` Kay Sievers
2004-02-23 8:48 ` Andrey Borzenkov
2004-02-28 15:15 ` Andrey Borzenkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040222094521.GC4873@localhost.localdomain \
--to=arvidjaar@mail.ru \
--cc=linux-hotplug@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).