From 98ccf6d67b6dd95c7471fd4e5a6d6452a62394b9 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 12 Feb 2008 09:24:09 +0100 Subject: [PATCH] Settings of the resolving and caching mechanism are now passed as parameters. --- lib/init.c | 22 +++++++++++---- lib/internal.h | 1 + lib/names-cache.c | 72 +++++++++++++++++++++++++---------------------- lib/names-net.c | 17 ++++------- lib/pci.h | 8 +----- 5 files changed, 62 insertions(+), 58 deletions(-) diff --git a/lib/init.c b/lib/init.c index 11a9af6..167c893 100644 --- a/lib/init.c +++ b/lib/init.c @@ -67,8 +67,11 @@ pci_alloc(void) memset(a, 0, sizeof(*a)); pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); - pci_set_net_domain(a, PCI_ID_DOMAIN, 0); +#ifdef PCI_USE_DNS + pci_define_param(a, "net.domain", PCI_ID_DOMAIN); + pci_define_param(a, "net.cache_path", "~/.pciids-cache"); a->id_lookup_mode = PCI_LOOKUP_CACHE; +#endif for(i=0; iconfig) pci_methods[i]->config(a); @@ -163,7 +166,7 @@ pci_define_param(struct pci_access *acc, char *param, char *value) } int -pci_set_param(struct pci_access *acc, char *param, char *value) +pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy) { struct pci_param *p; @@ -172,13 +175,22 @@ pci_set_param(struct pci_access *acc, char *param, char *value) { if (p->value_malloced) pci_mfree(p->value); - p->value_malloced = 1; - p->value = pci_strdup(acc, value); + p->value_malloced = copy; + if (copy) + p->value = pci_strdup(acc, value); + else + p->value = value; return 0; } return -1; } +int +pci_set_param(struct pci_access *acc, char *param, char *value) +{ + return pci_set_param_internal(acc, param, value, 1); +} + static void pci_free_params(struct pci_access *acc) { @@ -259,7 +271,5 @@ pci_cleanup(struct pci_access *a) pci_free_name_list(a); pci_free_params(a); pci_set_name_list_path(a, NULL, 0); - pci_set_net_domain(a, NULL, 0); - pci_set_id_cache(a, NULL, 0); pci_mfree(a); } diff --git a/lib/internal.h b/lib/internal.h index bc88f36..3299104 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -37,6 +37,7 @@ struct pci_dev *pci_alloc_dev(struct pci_access *); int pci_link_dev(struct pci_access *, struct pci_dev *); void pci_define_param(struct pci_access *acc, char *param, char *val); +int pci_set_param_internal(struct pci_access *acc, char *param, char *val, int copy); extern struct pci_methods pm_intel_conf1, pm_intel_conf2, pm_linux_proc, pm_fbsd_device, pm_aix_device, pm_nbsd_libpci, pm_obsd_device, diff --git a/lib/names-cache.c b/lib/names-cache.c index 73d09a2..dc75835 100644 --- a/lib/names-cache.c +++ b/lib/names-cache.c @@ -21,28 +21,40 @@ static const char cache_version[] = "#PCI-CACHE-1.0"; +static char *get_cache_name(struct pci_access *a) +{ + char *name, *buf; + + name = pci_get_param(a, "net.cache_path"); + if (!name || name[0]) + return NULL; + if (strncmp(name, "~/", 2)) + return name; + + uid_t uid = getuid(); + struct passwd *pw = getpwuid(uid); + if (!pw) + return name; + + buf = pci_malloc(a, strlen(pw->pw_dir) + strlen(name+1) + 1); + sprintf(buf, "%s%s", pw->pw_dir, name+1); + pci_set_param_internal(a, "net.cache_path", buf, 0); + return buf; +} + int pci_id_cache_load(struct pci_access *a, int flags) { char *name; char line[MAX_LINE]; - const char default_name[] = "/.pciids-cache"; FILE *f; int lino; a->id_cache_status = 1; - if (!a->id_cache_file) - { - /* Construct the default ID cache name */ - uid_t uid = getuid(); - struct passwd *pw = getpwuid(uid); - if (!pw) - return 0; - name = pci_malloc(a, strlen(pw->pw_dir) + sizeof(default_name)); - sprintf(name, "%s%s", pw->pw_dir, default_name); - pci_set_id_cache(a, name, 1); - } - a->debug("Using cache %s\n", a->id_cache_file); + name = get_cache_name(a); + if (!name) + return 0; + a->debug("Using cache %s\n", name); if (flags & PCI_LOOKUP_REFRESH_CACHE) { a->debug("Not loading cache, will refresh everything\n"); @@ -50,7 +62,7 @@ pci_id_cache_load(struct pci_access *a, int flags) return 0; } - f = fopen(a->id_cache_file, "rb"); + f = fopen(name, "rb"); if (!f) { a->debug("Cache file does not exist\n"); @@ -88,12 +100,12 @@ pci_id_cache_load(struct pci_access *a, int flags) } } } - a->warning("Malformed cache file %s (line %d), ignoring", a->id_cache_file, lino); + a->warning("Malformed cache file %s (line %d), ignoring", name, lino); break; } if (ferror(f)) - a->warning("Error while reading %s", a->id_cache_file); + a->warning("Error while reading %s", name); fclose(f); return 1; } @@ -105,13 +117,14 @@ pci_id_cache_flush(struct pci_access *a) FILE *f; unsigned int h; struct id_entry *e, *e2; - char hostname[256], *tmpname; + char hostname[256], *tmpname, *name; int this_pid; a->id_cache_status = 0; if (orig_status < 2) return; - if (!a->id_cache_file) + name = get_cache_name(a); + if (!name) return; this_pid = getpid(); @@ -119,17 +132,17 @@ pci_id_cache_flush(struct pci_access *a) hostname[0] = 0; else hostname[sizeof(hostname)-1] = 0; - tmpname = pci_malloc(a, strlen(a->id_cache_file) + strlen(hostname) + 64); - sprintf(tmpname, "%s.tmp-%s-%d", a->id_cache_file, hostname, this_pid); + tmpname = pci_malloc(a, strlen(name) + strlen(hostname) + 64); + sprintf(tmpname, "%s.tmp-%s-%d", name, hostname, this_pid); f = fopen(tmpname, "wb"); if (!f) { - a->warning("Cannot write to %s: %s", a->id_cache_file, strerror(errno)); + a->warning("Cannot write to %s: %s", name, strerror(errno)); pci_mfree(tmpname); return; } - a->debug("Writing cache to %s\n", a->id_cache_file); + a->debug("Writing cache to %s\n", name); fprintf(f, "%s\n", cache_version); for (h=0; hwarning("Error writing %s", a->id_cache_file); + a->warning("Error writing %s", name); fclose(f); - if (rename(tmpname, a->id_cache_file) < 0) + if (rename(tmpname, name) < 0) { - a->warning("Cannot rename %s to %s: %s", tmpname, a->id_cache_status, strerror(errno)); + a->warning("Cannot rename %s to %s: %s", tmpname, name, strerror(errno)); unlink(tmpname); } pci_mfree(tmpname); @@ -188,12 +201,3 @@ pci_id_cache_dirty(struct pci_access *a) if (a->id_cache_status >= 1) a->id_cache_status = 2; } - -void -pci_set_id_cache(struct pci_access *a, char *name, int to_be_freed) -{ - if (a->free_id_cache_file) - free(a->id_cache_file); - a->id_cache_file = name; - a->free_id_cache_file = to_be_freed; -} diff --git a/lib/names-net.c b/lib/names-net.c index 112e1ad..335bf68 100644 --- a/lib/names-net.c +++ b/lib/names-net.c @@ -21,13 +21,17 @@ char *pci_id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4) { - char name[256], dnsname[256], txt[256]; + char name[256], dnsname[256], txt[256], *domain; byte answer[4096]; const byte *data; int res, i, j, dlen; ns_msg m; ns_rr rr; + domain = pci_get_param(a, "net.domain"); + if (!domain || !domain[0]) + return NULL; + switch (cat) { case ID_VENDOR: @@ -54,7 +58,7 @@ char default: return NULL; } - sprintf(dnsname, "%s.%s", name, a->id_domain); + sprintf(dnsname, "%s.%s", name, domain); a->debug("Resolving %s\n", dnsname); res_init(); @@ -99,12 +103,3 @@ char *pci_id_net_lookup(struct pci_access *a UNUSED, int cat UNUSED, int id1 UNU } #endif - -void -pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed) -{ - if (a->free_id_domain) - free(a->id_domain); - a->id_domain = name; - a->free_id_domain = to_be_freed; -} diff --git a/lib/pci.h b/lib/pci.h index 1540ee1..030363a 100644 --- a/lib/pci.h +++ b/lib/pci.h @@ -48,11 +48,7 @@ struct pci_access { int numeric_ids; /* Enforce PCI_LOOKUP_NUMERIC (>1 => PCI_LOOKUP_MIXED) */ unsigned int id_lookup_mode; /* pci_lookup_mode flags which are set automatically */ - /* Default: PCI_LOOKUP_CACHE */ - char *id_domain; /* DNS domain used for the lookups (use pci_set_net_domain()) */ - int free_id_domain; /* Set if id_domain is malloced */ - char *id_cache_file; /* Name of the ID cache file (use pci_set_net_cache()) */ - int free_id_cache_file; /* Set if id_cache_file is malloced */ + /* Default: PCI_LOOKUP_CACHE */ int debugging; /* Turn on debugging messages */ @@ -189,8 +185,6 @@ char *pci_lookup_name(struct pci_access *a, char *buf, int size, int flags, ...) int pci_load_name_list(struct pci_access *a); /* Called automatically by pci_lookup_*() when needed; returns success */ void pci_free_name_list(struct pci_access *a); /* Called automatically by pci_cleanup() */ void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed); -void pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed); -void pci_set_id_cache(struct pci_access *a, char *name, int to_be_freed); void pci_id_cache_flush(struct pci_access *a); enum pci_lookup_mode { -- 2.39.2