diff options
author | Andreas Robinson <andr345@gmail.com> | 2009-05-14 10:15:59 +0200 |
---|---|---|
committer | Andreas Robinson <andr345@gmail.com> | 2009-05-15 14:56:29 +0200 |
commit | 97e2cb54ea7cea8e9b2af23c8d397427cb81e107 (patch) | |
tree | c0f63c5de33bce2f7a56bb2e33c756112b6d2435 | |
parent | 9054480bc36f701e572a1231c06b98f16646cce5 (diff) | |
download | module-init-tools-97e2cb54ea7cea8e9b2af23c8d397427cb81e107.tar.gz |
modinfo: use logging facility for oom-checks and error messages
Also fix slightly broken nofail_asprintf()
Side effects:
Errors are printed with the "ERROR:" prefix.
Testing for oom, quiets asprintf return-value warning.
Signed-off-by: Andreas Robinson <andr345@gmail.com>
-rw-r--r-- | logging.h | 6 | ||||
-rw-r--r-- | modinfo.c | 24 |
2 files changed, 17 insertions, 13 deletions
@@ -27,8 +27,10 @@ static inline void grammar(const char *cmd, #define NOFAIL(ptr) do_nofail((ptr), __FILE__, __LINE__, #ptr) -#define nofail_asprintf(ptr, ...) \ - { if (asprintf((ptr), __VA_ARGS__) < 0) do_nofail(NULL, __FILE__, __LINE__, #ptr); } +#define nofail_asprintf(ptr, ...) \ + do { if (asprintf((ptr), __VA_ARGS__) < 0) \ + do_nofail(NULL, __FILE__, __LINE__, #ptr); \ + } while(0) static inline void *do_nofail(void *ptr, const char *file, int line, const char *expr) { @@ -14,6 +14,7 @@ #include <sys/mman.h> #include "util.h" +#include "logging.h" #include "elfops.h" #include "zlibsupport.h" #include "testing.h" @@ -38,7 +39,7 @@ static struct param *add_param(const char *name, struct param **list) for (i = *list; i; i = i->next) if (strncmp(i->name, name, namelen) == 0) return i; - i = malloc(sizeof(*i) + namelen+1); + i = NOFAIL(malloc(sizeof(*i) + namelen+1)); strncpy((char *)(i + 1), name, namelen); ((char *)(i + 1))[namelen] = '\0'; i->name = (char *)(i + 1); @@ -188,10 +189,10 @@ static void *grab_module(const char *name, unsigned long *size, char**filename, if (strchr(name, '.') || strchr(name, '/')) { data = grab_file(name, size); if (data) { - *filename = strdup(name); + *filename = NOFAIL(strdup(name)); return data; } else { - fprintf(stderr, "modinfo: could not open %s: %s\n", + error("modinfo: could not open %s: %s\n", name, strerror(errno)); return NULL; } @@ -202,16 +203,16 @@ static void *grab_module(const char *name, unsigned long *size, char**filename, kernel = buf.release; } if (strlen(basedir)) - asprintf(&moddir, "%s/%s/%s", basedir, MODULE_DIR, kernel); + nofail_asprintf(&moddir, "%s/%s/%s", basedir, MODULE_DIR, kernel); else - asprintf(&moddir, "%s/%s", MODULE_DIR, kernel); + nofail_asprintf(&moddir, "%s/%s", MODULE_DIR, kernel); - asprintf(&depname, "%s/%s", moddir, "modules.dep"); + nofail_asprintf(&depname, "%s/%s", moddir, "modules.dep"); /* Search for it in modules.dep. */ data = grab_file(depname, size); if (!data) { - fprintf(stderr, "modinfo: could not open %s\n", depname); + error("modinfo: could not open %s\n", depname); free(depname); return NULL; } @@ -228,7 +229,7 @@ static void *grab_module(const char *name, unsigned long *size, char**filename, dir = moddir; /* new style - relative path */ if (strlen(dir)) { - asprintf(filename, "%s/%s", dir, p); + nofail_asprintf(filename, "%s/%s", dir, p); (*filename)[namelen + strlen(dir) + 1] = '\0'; } else { *filename = strndup(p, namelen); @@ -236,14 +237,13 @@ static void *grab_module(const char *name, unsigned long *size, char**filename, release_file(data, *size); data = grab_file(*filename, size); if (!data) - fprintf(stderr, - "modinfo: could not open %s: %s\n", + error("modinfo: could not open %s: %s\n", *filename, strerror(errno)); return data; } } release_file(data, *size); - fprintf(stderr, "modinfo: could not find module %s\n", name); + error("modinfo: could not find module %s\n", name); return NULL; } @@ -267,6 +267,8 @@ int main(int argc, char *argv[]) int opt, ret = 0; char *basedir = ""; + logging = 0; /* send messages to stderr */ + if (native_endianness() == 0) abort(); |