summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common.c61
-rw-r--r--common.h9
-rw-r--r--dmi.c2
-rw-r--r--pciusb.c4
4 files changed, 21 insertions, 55 deletions
diff --git a/common.c b/common.c
index 01ebae9..0f17cc7 100644
--- a/common.c
+++ b/common.c
@@ -6,6 +6,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <zlib.h>
#include "common.h"
static char *table_name_to_file(const char *name) {
@@ -15,60 +16,26 @@ static char *table_name_to_file(const char *name) {
if (!share_path || !*share_path) share_path = "/usr/share";
asprintf(&fname, "%s/ldetect-lst/%s", share_path, name);
+
return fname;
}
fh fh_open(const char *name) {
- fh ret;
char *fname = table_name_to_file(name);
- int length = strlen(fname);
-
- if (access(fname, R_OK) == 0) {
- ret.f = fopen(fname, "r");
- ret.pid = 0;
- } else {
- int fdno[2];
- char *fname_gz = alloca(length + sizeof(".gz"));
- sprintf(fname_gz, "%s.gz", fname);
- if (access(fname_gz, R_OK) != 0) {
- fprintf(stderr, "Missing %s (should be %s)\n", name, fname);
- exit(1);
- }
- if (pipe(fdno)) {
- perror("pciusb");
- exit(1);
- }
-
- if ((ret.pid = fork()) != 0) {
- ret.f = fdopen(fdno[0], "r");
- close(fdno[1]);
- } else {
- char* cmd[5];
- int ip = 0;
- char *ld_loader = getenv("LD_LOADER");
-
- if (ld_loader && *ld_loader)
- cmd[ip++] = ld_loader;
- cmd[ip++] = "gzip";
- cmd[ip++] = "-cd";
- cmd[ip++] = fname_gz;
- cmd[ip++] = NULL;
+ if (access(fname, R_OK) != 0) {
+ char *fname_gz;
+ asprintf(&fname_gz, "%s.gz", fname);
+ free(fname);
+ fname = fname_gz;
+ }
- dup2(fdno[1], STDOUT_FILENO);
- close(fdno[0]);
- close(fdno[1]);
- execvp(cmd[0], cmd);
- perror("pciusb");
- exit(2);
- }
+ fh f = gzopen(fname, "r");
+ if (!f) {
+ perror("pciusb");
+ exit(1);
}
- free(fname);
- return ret;
-}
-void fh_close(fh *f) {
- fclose(f->f);
- if (f->pid > 0)
- waitpid(f->pid, NULL, 0);
+ free(fname);
+ return f;
}
diff --git a/common.h b/common.h
index 324bbaf..1602cf3 100644
--- a/common.h
+++ b/common.h
@@ -2,6 +2,7 @@
#define LIBLDETECT_COMMON
#include "libldetect.h"
+#include <zlib.h>
#define NON_EXPORTED __attribute__((visibility("hidden")))
@@ -20,11 +21,9 @@ extern void pciusb_initialize(struct pciusb_entry *e) NON_EXPORTED;
#define psizeof(a) (sizeof(a) / sizeof(*(a)))
#define ifree(p) do { if (p) { free(p); p = NULL; } } while (0)
-typedef struct {
- FILE *f;
- pid_t pid;
-} fh;
+typedef gzFile fh;
extern fh fh_open(const char *name) NON_EXPORTED;
-extern void fh_close(fh *f) NON_EXPORTED;
+#define fh_gets(line, size, f) gzgets(f, line, size)
+#define fh_close(f) gzclose(f);
#endif
diff --git a/dmi.c b/dmi.c
index f4d3e86..d42568f 100644
--- a/dmi.c
+++ b/dmi.c
@@ -195,7 +195,7 @@ static struct dmi_entries entries_matching_criteria(struct criteria criteria) {
int previous_refine = 0;
- for (line = 1; fgets(buf, sizeof(buf) - 1, f.f); line++) {
+ for (line = 1; fh_gets(buf, sizeof(buf) - 1, f); line++) {
char *s = skip_leading_spaces(buf);
if (*s == '#') continue; // skip comments
diff --git a/pciusb.c b/pciusb.c
index 2397b5e..5cd367f 100644
--- a/pciusb.c
+++ b/pciusb.c
@@ -115,7 +115,7 @@ extern int pciusb_find_modules(struct pciusb_entries *entries, const char *fpciu
f = fh_open(fpciusbtable);
- for (line = 1; fgets(buf, sizeof(buf) - 1, f.f); line++) {
+ for (line = 1; fh_gets(buf, sizeof(buf) - 1, f); line++) {
unsigned short vendor, device, subvendor, subdevice;
char *p = NULL, *q = NULL;
int offset; unsigned int i;
@@ -162,7 +162,7 @@ extern int pciusb_find_modules(struct pciusb_entries *entries, const char *fpciu
e->already_found = 1;
}
}
- fh_close(&f);
+ fh_close(f);
/* If no special case in pcitable, then lookup modalias for PCI devices
(USB are already done by kernel)