From 20a2b71c9c4ed3c2dd781f6a57901514a3560c31 Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Tue, 23 Oct 2001 11:50:49 +0000 Subject: [PATCH] filter integration into tools --- include/.symlinks | 1 + lib/config/config.c | 12 +---- lib/device/dev-cache.c | 12 +++++ lib/filters/filter-persistent.c | 10 +++-- lib/filters/filter.c | 10 ++--- lib/filters/filter.h | 4 +- lib/mm/pool.c | 4 +- tools/lvm.c | 78 ++++++++++++++++++++++++++++----- tools/tools.h | 2 + 9 files changed, 99 insertions(+), 34 deletions(-) diff --git a/include/.symlinks b/include/.symlinks index a30c92b2e..be4517679 100644 --- a/include/.symlinks +++ b/include/.symlinks @@ -7,6 +7,7 @@ ../lib/device/dev-cache.h ../lib/device/device.h ../lib/display/display.h +../lib/filters/filter-composite.h ../lib/filters/filter-persistent.h ../lib/filters/filter-regex.h ../lib/filters/filter.h diff --git a/lib/config/config.c b/lib/config/config.c index 4c4c79fc0..b225289db 100644 --- a/lib/config/config.c +++ b/lib/config/config.c @@ -54,8 +54,6 @@ static struct config_node *_file(struct parser *p); static struct config_node *_section(struct parser *p); static struct config_value *_value(struct parser *p); static struct config_value *_type(struct parser *p); -static void _parse_error(struct parser *p, const char *file, int line, - const char *mess); static int _match_aux(struct parser *p, int t); static struct config_value *_create_value(struct parser *p); static struct config_node *_create_node(struct parser *p); @@ -66,7 +64,7 @@ static int _tok_match(const char *str, const char *b, const char *e); #define match(t) do {\ if (!_match_aux(p, (t))) {\ - _parse_error(p, __FILE__, __LINE__, "unexpected token"); \ + log_error("Parse error at line %d: unexpected token", p->line); \ return 0;\ } \ } while(0); @@ -349,18 +347,12 @@ static struct config_value *_type(struct parser *p) { break; default: - _parse_error(p, __FILE__, __LINE__, "expected a value"); + log_error("Parse error at line %d: expected a value", p->line); return 0; } return v; } -static void _parse_error(struct parser *p, const char *file, int line, - const char *mess) -{ - plog(_LOG_ERR, file, line, "parse error at %d: %s", p->line, mess); -} - static int _match_aux(struct parser *p, int t) { if (p->t != t) diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index 4cda83f6f..974751f08 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -238,6 +238,18 @@ void dev_cache_exit(void) int dev_cache_add_dir(const char *path) { struct dir_list *dl; + struct stat st; + + if (stat(path, &st)) { + log_error("Ignoring %s: %s", path, strerror(errno)); + /* But don't fail */ + return 1; + } + + if (!S_ISDIR(st.st_mode)) { + log_error("Ignoring %s: Not a directory", path); + return 1; + } if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1))) return 0; diff --git a/lib/filters/filter-persistent.c b/lib/filters/filter-persistent.c index 457c2cf21..6fd2c6337 100644 --- a/lib/filters/filter-persistent.c +++ b/lib/filters/filter-persistent.c @@ -55,7 +55,7 @@ static int _load(struct pfilter *pf) } if (!(cn = find_config_node(cf->root, "/valid_devices", '/'))) { - log_info("couldn't find 'valid_devices' array in '%s'", + log_info("Couldn't find 'valid_devices' array in '%s'", pf->file); goto out; } @@ -66,13 +66,13 @@ static int _load(struct pfilter *pf) */ for (cv = cn->v; cv; cv = cv->next) { if (cv->type != CFG_STRING) { - log_info("valid_devices array contains a value " + log_info("Valid_devices array contains a value " "which is not a string ... ignoring"); continue; } if (!hash_insert(pf->devices, cv->v.str, PF_UNCHECKED)) - log_info("couldn't add '%s' to filter ... ignoring", + log_info("Couldn't add '%s' to filter ... ignoring", cv->v.str); } r = 1; @@ -88,6 +88,8 @@ static int _dump(struct pfilter *pf) struct hash_node *n; FILE *fp = fopen(pf->file, "w"); + log_very_verbose("Dumping persistent device cache to %s", pf->file); + if (!fp) { log_info("Couldn't open '%s' for to hold valid devices.", pf->file); @@ -118,6 +120,8 @@ static int _check(const char *path) if (fd >= 0) r = 1; + else + log_debug("Unable to open %s: %s", path, strerror(errno)); close(fd); return r; diff --git a/lib/filters/filter.c b/lib/filters/filter.c index 0d1766a2a..f25744c64 100644 --- a/lib/filters/filter.c +++ b/lib/filters/filter.c @@ -54,10 +54,8 @@ static device_info_t device_info[] = { static int *scan_proc_dev(void); -static int passes_config_device_filter(struct dev_filter *f, struct device *dev) +static int passes_lvm_type_device_filter(struct dev_filter *f, struct device *dev) { - /* FIXME Check against config file scan/reject entries */ - /* Is this a recognised device type? */ if (!(((int *) f->private)[MAJOR(dev->dev)])) return 0; @@ -65,7 +63,7 @@ static int passes_config_device_filter(struct dev_filter *f, struct device *dev) return 1; } -struct dev_filter *config_filter_create() +struct dev_filter *lvm_type_filter_create() { struct dev_filter *f; @@ -74,7 +72,7 @@ struct dev_filter *config_filter_create() return NULL; } - f->passes_filter = passes_config_device_filter; + f->passes_filter = passes_lvm_type_device_filter; if (!(f->private = scan_proc_dev())) return NULL; @@ -82,7 +80,7 @@ struct dev_filter *config_filter_create() return f; } -void config_filter_destroy(struct dev_filter *f) +void lvm_type_filter_destroy(struct dev_filter *f) { dbg_free(f->private); dbg_free(f); diff --git a/lib/filters/filter.h b/lib/filters/filter.h index 582a44327..e655599bf 100644 --- a/lib/filters/filter.h +++ b/lib/filters/filter.h @@ -21,9 +21,9 @@ #ifndef _LVM_FILTER_H #define _LVM_FILTER_H -struct dev_filter *config_filter_create(); +struct dev_filter *lvm_type_filter_create(); -void config_filter_destroy(struct dev_filter *f); +void lvm_type_filter_destroy(struct dev_filter *f); #endif diff --git a/lib/mm/pool.c b/lib/mm/pool.c index 53cc8f7ee..65fb3f6bd 100644 --- a/lib/mm/pool.c +++ b/lib/mm/pool.c @@ -36,7 +36,7 @@ struct pool *pool_create(size_t chunk_hint) struct pool *p = dbg_malloc(sizeof(*p)); if (!p) { - log_err("couldn't create pool"); + log_error("Couldn't create memory pool"); return 0; } memset(p, 0, sizeof(*p)); @@ -115,7 +115,7 @@ void pool_free(struct pool *p, void *ptr) } if (!c) - log_warn("pool_free asked to free a pointer " + log_debug("pool_free asked to free a pointer " "that wasn't in the pool, doing nothing"); else p->chunk = c; diff --git a/tools/lvm.c b/tools/lvm.c index 30a1a83e9..080fe4abc 100644 --- a/tools/lvm.c +++ b/tools/lvm.c @@ -559,7 +559,7 @@ static void __init_log(struct config_file *cf) if (log_file) { /* set up the logging */ if (!(_log = fopen(log_file, "w"))) - log_error("couldn't open log file %s\n", log_file); + log_error("Couldn't open log file %s", log_file); else init_log(_log); } @@ -568,6 +568,67 @@ static void __init_log(struct config_file *cf) init_debug(_debug_level); } +static int dev_cache_setup(void) +{ + struct config_node *cn; + struct config_value *cv; + + if (!dev_cache_init()) { + stack; + return 0; + } + + if (!(cn = find_config_node(_cf->root, "devices/scan", '/'))) { + if (!dev_cache_add_dir("/dev")) { + log_error("Failed to add /dev to internal " + "device cache"); + return 0; + } + } + + for (cv = cn->v; cv; cv = cv->next) { + if (cv->type != CFG_STRING) { + log_error("Invalid string in config file: " + "devices/scan"); + return 0; + } + + if (!dev_cache_add_dir(cv->v.str)) { + log_error("Failed to add %s to internal device cache", + cv->v.str); + return 0; + } + } + + return 1; +} + +static struct dev_filter *filter_setup(void) +{ + struct config_node *cn; + struct dev_filter *f1, *f2, *f3, *f4; + + if (!(f1 = lvm_type_filter_create())) + return 0; + + if (!(cn = find_config_node(_cf->root, "devices/filter", '/'))) { + log_debug("devices/filter not found in config file"); + return f1; + } + + if (!(f2 = regex_filter_create(cn->v))) { + log_error("Failed to create regex device filter"); + return f1; + } + + if (!(f4 = composite_filter_create(2, f1, f2))) { + log_error("Failed to create composite device filter"); + return f1; + } + + return f4; +} + static int init(void) { int ret = 0; @@ -593,25 +654,20 @@ static int init(void) if (stat(e, &info) != -1) { /* we've found a config file */ if (!read_config(_cf, e)) { - stack; + log_error("Failed to load config file %s", e); goto out; } __init_log(_cf); } - if (!dev_cache_init()) { - stack; - goto out; - } - if (!dev_cache_add_dir("/dev")) { - log_error("Failed to add %s to internal device cache", prefix); + if (!dev_cache_setup()) { goto out; } - if (!(_filter = config_filter_create())) { - /* Add scan & rejects from _cf->root */ + if (!(_filter = filter_setup())) { + log_error("Failed to set up internal device filters"); goto out; } @@ -643,7 +699,7 @@ static void __fin_commands(void) static void fin(void) { ios->destroy(ios); - config_filter_destroy(_filter); + lvm_type_filter_destroy(_filter); dev_cache_exit(); destroy_config_file(_cf); __fin_commands(); diff --git a/tools/tools.h b/tools/tools.h index aab155f70..d3caf1245 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -38,6 +38,8 @@ #include "display.h" #include "errors.h" #include "filter.h" +#include "filter-composite.h" +#include "filter-regex.h" #include "format1.h" #include "toollib.h" #include "activate.h" -- 2.43.5