char * pci_strdup(struct pci_access *a, char *s) { int len = strlen(s) + 1; char *t = pci_malloc(a, len); memcpy(t, s, len); return t; }
static void dump_alloc_data(struct pci_dev *dev, int len) { struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1); dd->allocated = len; dd->len = 0; memset(dd->data, 0xff, len); dev->aux = dd; }
struct pci_dev * pci_alloc_dev(struct pci_access *a) { struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev)); memset(d, 0, sizeof(*d)); d->access = a; d->methods = a->methods; if (d->methods->init_dev) d->methods->init_dev(d); return d; }
static void pci_add_cap(struct pci_dev *d, unsigned int addr, unsigned int id, unsigned int type) { struct pci_cap *cap = pci_malloc(d->access, sizeof(*cap)); cap->next = d->first_cap; d->first_cap = cap; cap->addr = addr; cap->id = id; cap->type = type; d->access->debug("%04x:%02x:%02x.%d: Found capability %04x of type %d at %04x\n", d->domain, d->bus, d->dev, d->func, id, type, addr); }
struct pci_dev * pci_alloc_dev(struct pci_access *a) { struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev)); bzero(d, sizeof(*d)); d->access = a; d->methods = a->methods; d->hdrtype = -1; if (d->methods->init_dev) d->methods->init_dev(d); return d; }
static void dump_init(struct pci_access *a) { char *name = a->method_params[PCI_ACCESS_DUMP]; FILE *f; char buf[256]; struct pci_dev *dev = NULL; int len, bn, dn, fn, i, j; if (!a) a->error("dump: File name not given."); if (!(f = fopen(name, "r"))) a->error("dump: Cannot open %s: %s", name, strerror(errno)); while (fgets(buf, sizeof(buf)-1, f)) { char *z = strchr(buf, '\n'); if (!z) a->error("dump: line too long or unterminated"); *z-- = 0; if (z >= buf && *z == '\r') *z-- = 0; len = z - buf + 1; if (len >= 8 && buf[2] == ':' && buf[5] == '.' && buf[7] == ' ' && sscanf(buf, "%x:%x.%d ", &bn, &dn, &fn) == 3) { dev = pci_get_dev(a, bn, dn, fn); dev->aux = pci_malloc(a, 256); memset(dev->aux, 0xff, 256); pci_link_dev(a, dev); } else if (!len) dev = NULL; else if (dev && len >= 51 && buf[2] == ':' && buf[3] == ' ' && sscanf(buf, "%x: ", &i) == 1) { z = buf+3; while (isspace(z[0]) && isxdigit(z[1]) && isxdigit(z[2])) { z++; if (sscanf(z, "%x", &j) != 1 || i >= 256) a->error("dump: Malformed line"); ((byte *) dev->aux)[i++] = j; z += 2; } } } }
static int aix_detect(struct pci_access *a) { int len; int mode = a->writeable ? W_OK : R_OK; char *command = AIX_LSDEV_CMD; FILE *lsdev_pipe; char buf[256]; char *name; lsdev_pipe = popen(command, "r"); if (lsdev_pipe == NULL) { a->error("aix_config: popen(\"%s\") failed", command); } while (fgets(buf, sizeof (buf) - 1, lsdev_pipe) != NULL) { len = strlen(buf); while (buf[len-1] == '\n' || buf[len-1] == '\r') len--; buf[len] = '\0'; /* clobber the newline */ name = (char *) pci_malloc(a, len + 1); strcpy(name, buf); pci_buses[pci_bus_count].bus_name = name; pci_buses[pci_bus_count].bus_number = 0; pci_buses[pci_bus_count].bus_fd = -1; if (!pci_bus_count) a->debug("...using %s", name); else a->debug(", %s", name); pci_bus_count++; if (pci_bus_count >= PCI_BUS_MAX) break; } (void) pclose(lsdev_pipe); return pci_bus_count; }
static char *get_cache_name(struct pci_access *a) { char *name, *buf; name = pci_get_param(a, "net.cache_name"); 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_name", buf, 1); pci_mfree(buf); return pci_get_param(a, "net.cache_name"); }
void pci_id_cache_flush(struct pci_access *a) { int orig_status = a->id_cache_status; FILE *f; unsigned int h; struct id_entry *e, *e2; char hostname[256], *tmpname, *name; int this_pid; a->id_cache_status = 0; if (orig_status < 2) return; name = get_cache_name(a); if (!name) return; this_pid = getpid(); if (gethostname(hostname, sizeof(hostname)) < 0) hostname[0] = 0; else hostname[sizeof(hostname)-1] = 0; 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", name, strerror(errno)); pci_mfree(tmpname); return; } a->debug("Writing cache to %s\n", name); fprintf(f, "%s\n", cache_version); for (h=0; h<HASH_SIZE; h++) for (e=a->id_hash[h]; e; e=e->next) if (e->src == SRC_CACHE || e->src == SRC_NET) { /* Negative entries are not written */ if (!e->name[0]) continue; /* Verify that every entry is written at most once */ for (e2=a->id_hash[h]; e2 != e; e2=e2->next) if ((e2->src == SRC_CACHE || e2->src == SRC_NET) && e2->cat == e->cat && e2->id12 == e->id12 && e2->id34 == e->id34) break; if (e2 == e) fprintf(f, "%d %x %x %x %x %s\n", e->cat, pair_first(e->id12), pair_second(e->id12), pair_first(e->id34), pair_second(e->id34), e->name); } fflush(f); if (ferror(f)) a->warning("Error writing %s", name); fclose(f); if (rename(tmpname, name) < 0) { a->warning("Cannot rename %s to %s: %s", tmpname, name, strerror(errno)); unlink(tmpname); } pci_mfree(tmpname); }