]> mj.ucw.cz Git - pciutils.git/commitdiff
Settings of the resolving and caching mechanism are now passed as parameters.
authorMartin Mares <mj@ucw.cz>
Tue, 12 Feb 2008 08:24:09 +0000 (09:24 +0100)
committerMartin Mares <mj@ucw.cz>
Tue, 12 Feb 2008 08:24:09 +0000 (09:24 +0100)
lib/init.c
lib/internal.h
lib/names-cache.c
lib/names-net.c
lib/pci.h

index 11a9af62cc96d95e28e69da4a446336f26408c4f..167c893131ac04e63a1fe7c2382866bb41b7a90b 100644 (file)
@@ -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; i<PCI_ACCESS_MAX; i++)
     if (pci_methods[i] && pci_methods[i]->config)
       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);
 }
index bc88f367a67cd2dfd4741f790acf084ac7acc0c2..329910450e8dfe9ed9e04d63113b92e7e8dab7d2 100644 (file)
@@ -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,
index 73d09a2e09c89cb4c3e8976ef860070dca88cd4e..dc75835a671fed72d92b4ea4172aa0935d97b0c2 100644 (file)
 
 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; h<HASH_SIZE; h++)
@@ -156,12 +169,12 @@ pci_id_cache_flush(struct pci_access *a)
 
   fflush(f);
   if (ferror(f))
-    a->warning("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;
-}
index 112e1ad4af73c26fb24b19eae9b9fee7a964f659..335bf682212495d1da20b35b1850ac6d5bb1a91f 100644 (file)
 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;
-}
index 1540ee127472ddd3765b90a4475afc4418906ac5..030363ac18ed166e3a37be98b70dd2f89ee4efe9 100644 (file)
--- 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 {