From: Karel Zak Date: Mon, 13 Sep 2010 15:06:18 +0000 (+0200) Subject: libmount: rewrite DBG() macro X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f31a9593b21dd08b5f4469f9ff88dc12dbfb63d;p=util-linux libmount: rewrite DBG() macro Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/cache.c b/shlibs/mount/src/cache.c index 7e5c4afe..a341efd4 100644 --- a/shlibs/mount/src/cache.c +++ b/shlibs/mount/src/cache.c @@ -69,7 +69,11 @@ struct _mnt_cache { */ mnt_cache *mnt_new_cache(void) { - return calloc(1, sizeof(struct _mnt_cache)); + mnt_cache *cache = calloc(1, sizeof(struct _mnt_cache)); + if (!cache) + return NULL; + DBG(CACHE, mnt_debug_h(cache, "alloc")); + return cache; } /** @@ -84,6 +88,9 @@ void mnt_free_cache(mnt_cache *cache) if (!cache) return; + + DBG(CACHE, mnt_debug_h(cache, "free")); + for (i = 0; i < cache->nents; i++) { struct mnt_cache_entry *e = &cache->ents[i]; if (e->real != e->native) @@ -122,8 +129,7 @@ static int mnt_cache_add_entry(mnt_cache *cache, char *native, e->flag = flag; cache->nents++; - DBG(DEBUG_CACHE, - printf("cache: add entry[%2zd] (%s): %s: %s\n", + DBG(CACHE, mnt_debug_h(cache, "add entry [%2zd] (%s): %s: %s", cache->nents, (flag & MNT_CACHE_ISPATH) ? "path" : "tag", real, native)); @@ -249,7 +255,7 @@ int mnt_cache_read_tags(mnt_cache *cache, const char *devname) if (!cache || !devname) return -EINVAL; - DBG(DEBUG_CACHE, printf("cache: tags for %s requested\n", devname)); + DBG(CACHE, mnt_debug_h(cache, "tags for %s requested", devname)); /* check is device is already cached */ for (i = 0; i < cache->nents; i++) { @@ -261,8 +267,7 @@ int mnt_cache_read_tags(mnt_cache *cache, const char *devname) return 0; } - DBG(DEBUG_CACHE, - printf("cache: reading tags for: %s\n", devname)); + DBG(CACHE, mnt_debug_h(cache, "reading tags for: %s", devname)); pr = blkid_new_probe_from_filename(devname); if (!pr) diff --git a/shlibs/mount/src/init.c b/shlibs/mount/src/init.c index 68c13f94..2dfab58f 100644 --- a/shlibs/mount/src/init.c +++ b/shlibs/mount/src/init.c @@ -12,6 +12,7 @@ */ #include +#include #include "mountP.h" @@ -29,7 +30,7 @@ int libmount_debug_mask; */ void mnt_init_debug(int mask) { - if (libmount_debug_mask & DEBUG_INIT) + if (libmount_debug_mask & MNT_DEBUG_INIT) return; if (!mask) { char *str = mnt_getenv_safe("LIBMOUNT_DEBUG"); @@ -41,5 +42,5 @@ void mnt_init_debug(int mask) if (libmount_debug_mask) printf("libmount: debug mask set to 0x%04x.\n", libmount_debug_mask); - libmount_debug_mask |= DEBUG_INIT; + libmount_debug_mask |= MNT_DEBUG_INIT; } diff --git a/shlibs/mount/src/lock.c b/shlibs/mount/src/lock.c index 766b9e41..b168526c 100644 --- a/shlibs/mount/src/lock.c +++ b/shlibs/mount/src/lock.c @@ -74,6 +74,8 @@ mnt_lock *mnt_new_lock(const char *datafile, pid_t id) ml->lockfile_fd = -1; ml->linkfile = ln; ml->lockfile = lo; + + DBG(LOCKS, mnt_debug_h(ml, "alloc")); return ml; err: free(lo); @@ -93,6 +95,7 @@ void mnt_free_lock(mnt_lock *ml) { if (!ml) return; + DBG(LOCKS, mnt_debug_h(ml, "free")); free(ml->lockfile); free(ml->linkfile); free(ml); @@ -152,8 +155,7 @@ static int mnt_wait_lock(mnt_lock *ml, struct flock *fl, time_t maxtime) sigaction(SIGALRM, &sa, &osa); - DBG(DEBUG_LOCKS, fprintf(stderr, - "LOCK: (%d) waiting for F_SETLKW.\n", getpid())); + DBG(LOCKS, mnt_debug_h(ml, "(%d) waiting for F_SETLKW", getpid())); alarm(maxtime - now.tv_sec); if (fcntl(ml->lockfile_fd, F_SETLKW, fl) == -1) @@ -163,8 +165,8 @@ static int mnt_wait_lock(mnt_lock *ml, struct flock *fl, time_t maxtime) /* restore old sigaction */ sigaction(SIGALRM, &osa, NULL); - DBG(DEBUG_LOCKS, fprintf(stderr, - "LOCK: (%d) leaving mnt_wait_setlkw(), rc=%d.\n", getpid(), ret)); + DBG(LOCKS, mnt_debug_h(ml, "(%d) leaving mnt_wait_setlkw(), rc=%d", + getpid(), ret)); return ret; } @@ -229,8 +231,7 @@ void mnt_unlock_file(mnt_lock *ml) if (!ml) return; - DBG(DEBUG_LOCKS, fprintf(stderr, - "LOCK: (%d) unlocking/cleaning.\n", getpid())); + DBG(LOCKS, mnt_debug_h(ml, "(%d) unlocking/cleaning", getpid())); if (ml->locked == 0 && ml->lockfile && ml->linkfile) { @@ -393,7 +394,7 @@ int mnt_lock_file(mnt_lock *ml) if (ml->locked) { /* We made the link. Now claim the lock. */ if (fcntl (ml->lockfile_fd, F_SETLK, &flock) == -1) { - DBG(DEBUG_LOCKS, fprintf(stderr, + DBG(LOCKS, mnt_debug_h(ml, "%s: can't F_SETLK lockfile, errno=%d\n", lockfile, errno)); /* proceed, since it was us who created the lockfile anyway */ @@ -404,7 +405,7 @@ int mnt_lock_file(mnt_lock *ml) int err = mnt_wait_lock(ml, &flock, maxtime.tv_sec); if (err == 1) { - DBG(DEBUG_LOCKS, fprintf(stderr, + DBG(LOCKS, mnt_debug_h(ml, "%s: can't create link: time out (perhaps " "there is a stale lock file?)", lockfile)); rc = -ETIMEDOUT; @@ -419,7 +420,7 @@ int mnt_lock_file(mnt_lock *ml) ml->lockfile_fd = -1; } } - DBG(DEBUG_LOCKS, fprintf(stderr, + DBG(LOCKS, mnt_debug_h(ml, "LOCK: %s: (%d) successfully locked\n", lockfile, getpid())); unlink(linkfile); diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 444d0d77..ff3d21dc 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -34,21 +34,51 @@ #define CONFIG_LIBMOUNT_DEBUG #endif -#define DEBUG_INIT (1 << 1) -#define DEBUG_CACHE (1 << 2) -#define DEBUG_OPTIONS (1 << 3) -#define DEBUG_LOCKS (1 << 4) -#define DEBUG_TAB (1 << 5) -#define DEBUG_MTAB (1 << 6) -#define DEBUG_UTILS (1 << 7) -#define DEBUG_ALL 0xFFFF +#define MNT_DEBUG_INIT (1 << 1) +#define MNT_DEBUG_CACHE (1 << 2) +#define MNT_DEBUG_OPTIONS (1 << 3) +#define MNT_DEBUG_LOCKS (1 << 4) +#define MNT_DEBUG_TAB (1 << 5) +#define MNT_DEBUG_MTAB (1 << 6) +#define MNT_DEBUG_UTILS (1 << 7) +#define MNT_DEBUG_CXT (1 << 8) +#define MNT_DEBUG_ALL 0xFFFF #ifdef CONFIG_LIBMOUNT_DEBUG -#include +# include +# include + +# define DBG(m,x) do { \ + if ((MNT_DEBUG_ ## m) & libmount_debug_mask) {\ + fprintf(stderr, "libmount: %s: ", # m); \ + x; \ + } \ + } while(0) + extern int libmount_debug_mask; -#define DBG(m,x) if ((m) & libmount_debug_mask) x -#else -#define DBG(m,x) + +static inline void mnt_debug(const char *mesg, ...) +{ + va_list ap; + va_start(ap, mesg); + vfprintf(stderr, mesg, ap); + va_end(ap); + fputc('\n', stderr); +} + +static inline void mnt_debug_h(void *handler, const char *mesg, ...) +{ + va_list ap; + + fprintf(stderr, "[%p]: ", handler); + va_start(ap, mesg); + vfprintf(stderr, mesg, ap); + va_end(ap); + fputc('\n', stderr); +} + +#else /* !CONFIG_LIBMOUNT_DEBUG */ +# define DBG(m,x) #endif /* extension for files in the /etc/fstab.d directory */ diff --git a/shlibs/mount/src/optstr.c b/shlibs/mount/src/optstr.c index 97ee46a3..2b5d46a9 100644 --- a/shlibs/mount/src/optstr.c +++ b/shlibs/mount/src/optstr.c @@ -86,8 +86,7 @@ static int mnt_optstr_parse_next(char **optstr, char **name, size_t *namesz, return 1; /* end of optstr */ error: - DBG(DEBUG_OPTIONS, fprintf(stderr, - "libmount: parse error: \"%s\"\n", optstr0)); + DBG(OPTIONS, mnt_debug("parse error: \"%s\"", optstr0)); return -EINVAL; } @@ -124,8 +123,7 @@ static int mnt_optstr_locate_option(char *optstr, const char *name, char **begin } } while(1); - DBG(DEBUG_OPTIONS, fprintf(stderr, - "libmount: can't found '%s' option\n", name)); + DBG(OPTIONS, mnt_debug("can't found '%s' option", name)); return rc; } @@ -455,8 +453,7 @@ int mnt_optstr_get_mountflags(const char *optstr, unsigned long *flags) } } - DBG(DEBUG_OPTIONS, fprintf(stderr, - "libmount: optstr '%s': mountflags 0x%08lx\n", optstr, *flags)); + DBG(OPTIONS, mnt_debug("%s: mountflags 0x%08lx", optstr, *flags)); return 0; } diff --git a/shlibs/mount/src/tab.c b/shlibs/mount/src/tab.c index 943175bc..d5bde746 100644 --- a/shlibs/mount/src/tab.c +++ b/shlibs/mount/src/tab.c @@ -75,7 +75,7 @@ mnt_tab *mnt_new_tab(void) if (!tb) return NULL; - DBG(DEBUG_TAB, fprintf(stderr, "libmount: new tab %p\n", tb)); + DBG(TAB, mnt_debug_h(tb, "alloc")); INIT_LIST_HEAD(&tb->ents); return tb; @@ -92,6 +92,8 @@ void mnt_free_tab(mnt_tab *tb) if (!tb) return; + DBG(TAB, mnt_debug_h(tb, "free")); + while (!list_empty(&tb->ents)) { mnt_fs *fs = list_entry(tb->ents.next, mnt_fs, ents); mnt_free_fs(fs); @@ -168,11 +170,8 @@ int mnt_tab_add_fs(mnt_tab *tb, mnt_fs *fs) list_add_tail(&fs->ents, &tb->ents); - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: add entry: %s %s\n", - tb, mnt_fs_get_source(fs), - mnt_fs_get_target(fs))); - + DBG(TAB, mnt_debug_h(tb, "add entry: %s %s", + mnt_fs_get_source(fs), mnt_fs_get_target(fs))); tb->nents++; return 0; } @@ -215,7 +214,7 @@ int mnt_tab_get_root_fs(mnt_tab *tb, mnt_fs **root) if (!tb || !root) return -EINVAL; - DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: lookup root fs\n", tb)); + DBG(TAB, mnt_debug_h(tb, "lookup root fs", tb)); mnt_reset_iter(&itr, MNT_ITER_FORWARD); while(mnt_tab_next_fs(tb, &itr, &fs) == 0) { @@ -253,9 +252,8 @@ int mnt_tab_next_child_fs(mnt_tab *tb, mnt_iter *itr, if (!tb || !itr || !parent) return -EINVAL; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup next child of %s\n", - tb, mnt_fs_get_target(parent))); + DBG(TAB, mnt_debug_h(tb, "lookup next child of %s", + mnt_fs_get_target(parent))); parent_id = mnt_fs_get_id(parent); if (!parent_id) @@ -360,8 +358,7 @@ int mnt_tab_find_next_fs(mnt_tab *tb, mnt_iter *itr, if (!tb || !itr || !fs || !match_func) return -EINVAL; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup next fs\n", tb)); + DBG(TAB, mnt_debug_h(tb, "lookup next fs", tb)); if (!itr->head) MNT_ITER_INIT(itr, &tb->ents); @@ -430,8 +427,7 @@ mnt_fs *mnt_tab_find_target(mnt_tab *tb, const char *path, int direction) if (!tb || !path) return NULL; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup target: %s\n", tb, path)); + DBG(TAB, mnt_debug_h(tb, "lookup target: %s", path)); /* native @target */ mnt_reset_iter(&itr, direction); @@ -488,8 +484,7 @@ mnt_fs *mnt_tab_find_srcpath(mnt_tab *tb, const char *path, int direction) assert(tb); assert(path); - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup srcpath: %s\n", tb, path)); + DBG(TAB, mnt_debug_h(tb, "lookup srcpath: %s", path)); /* native paths */ mnt_reset_iter(&itr, direction); @@ -590,8 +585,7 @@ mnt_fs *mnt_tab_find_tag(mnt_tab *tb, const char *tag, if (!tb || !tag || !val) return NULL; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup by TAG: %s %s\n", tb, tag, val)); + DBG(TAB, mnt_debug_h(tb, "lookup by TAG: %s %s", tag, val)); /* look up by TAG */ mnt_reset_iter(&itr, direction); @@ -633,8 +627,7 @@ mnt_fs *mnt_tab_find_source(mnt_tab *tb, const char *source, int direction) if (!tb || !source) return NULL; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: lookup SOURCE: %s\n", tb, source)); + DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: %s", source)); if (strchr(source, '=')) { char *tag, *val; diff --git a/shlibs/mount/src/tab_parse.c b/shlibs/mount/src/tab_parse.c index 0ea72a2e..9c65c57e 100644 --- a/shlibs/mount/src/tab_parse.c +++ b/shlibs/mount/src/tab_parse.c @@ -334,13 +334,12 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, /* Missing final newline? Otherwise extremely */ /* long line - assume file was corrupted */ if (feof(f)) { - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: WARNING: no final newline at the end of %s\n", - filename)); + DBG(TAB, mnt_debug_h(tb, + "%s: no final newline", filename)); s = index (buf, '\0'); } else { - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: %s: %d: missing newline at line\n", + DBG(TAB, mnt_debug_h(tb, + "%s:%d: missing newline at line", filename, *nlines)); goto err; } @@ -351,8 +350,7 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, s = skip_spaces(buf); } while (*s == '\0' || *s == '#'); - DBG(DEBUG_TAB, fprintf(stderr, "libmount: %s:%d: %s\n", - filename, *nlines, s)); + DBG(TAB, mnt_debug_h(tb, "%s:%d: %s", filename, *nlines, s)); if (!tb->fmt) tb->fmt = detect_fmt(s); @@ -375,17 +373,15 @@ static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, return -ENOMEM; } - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: %s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, " - "OPTS:%s, FREQ:%d, PASSNO:%d\n", - tb, filename, *nlines, + DBG(TAB, mnt_debug_h(tb, "%s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, " + "OPTS:%s, FREQ:%d, PASSNO:%d", + filename, *nlines, fs->source, fs->target, fs->fstype, fs->optstr, fs->freq, fs->passno)); return 0; err: - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: %s:%d: parse error\n", tb, filename, *nlines)); + DBG(TAB, mnt_debug_h(tb, "%s:%d: parse error", tb, filename, *nlines)); rc = 1; /* recoverable error */ @@ -411,8 +407,7 @@ int mnt_tab_parse_stream(mnt_tab *tb, FILE *f, const char *filename) assert(f); assert(filename); - DBG(DEBUG_TAB, - fprintf(stderr, "libmount: tab %p: start parsing %s\n", tb, filename)); + DBG(TAB, mnt_debug_h(tb, "%s: start parsing", filename)); while (!feof(f)) { mnt_fs *fs = mnt_new_fs(); @@ -433,12 +428,10 @@ int mnt_tab_parse_stream(mnt_tab *tb, FILE *f, const char *filename) } } - DBG(DEBUG_TAB, - fprintf(stderr, "libmount: tab %p: stop parsing %s\n", tb, filename)); + DBG(TAB, mnt_debug_h(tb, "%s: stop parsing", filename)); return 0; err: - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: error parsing %s (rc=%d)\n", tb, filename, rc)); + DBG(TAB, mnt_debug_h(tb, "%s: parse error (rc=%d)", filename, rc)); return rc; } @@ -618,8 +611,7 @@ done: num = mnt_tab_get_nents(tb) - num; - DBG(DEBUG_TAB, fprintf(stderr, - "libmount: tab %p: fstab contains %d records\n", tb, num)); + DBG(TAB, mnt_debug_h(tb, "fstab contains %d records", num)); return num > 0 ? 0 : -1; } diff --git a/shlibs/mount/src/tab_update.c b/shlibs/mount/src/tab_update.c index 7b53d63c..c023777c 100644 --- a/shlibs/mount/src/tab_update.c +++ b/shlibs/mount/src/tab_update.c @@ -82,8 +82,7 @@ mnt_update *mnt_new_update(int action, unsigned long mountflags, const mnt_fs *f if (!upd) return NULL; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: allocate\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "allocate")); if (action) mnt_update_set_action(upd, action); @@ -105,8 +104,7 @@ void mnt_free_update(mnt_update *upd) if (!upd) return; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: deallacate\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "free")); mnt_free_lock(upd->lc); free(upd->filename); @@ -378,8 +376,7 @@ static int update_file(const char *filename, int fmt, mnt_tab *tb) if (!tb) goto error; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %s: update from tab %p\n", filename, tb)); + DBG(MTAB, mnt_debug("%s: update from tab %p", filename, tb)); if (snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename) >= sizeof(tmpname)) @@ -416,8 +413,7 @@ static int update_file(const char *filename, int fmt, mnt_tab *tb) return 0; error: - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %s: update from tab %p failed\n", filename, tb)); + DBG(MTAB, mnt_debug("%s: update from tab %p failed", filename, tb)); if (f) fclose(f); return -1; @@ -550,9 +546,10 @@ int mnt_prepare_update(mnt_update *upd) if (!upd || !upd->fs) return -1; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: prepare update (target %s, source %s, optstr %s)\n", - upd, mnt_fs_get_target(upd->fs), mnt_fs_get_source(upd->fs), + DBG(MTAB, mnt_debug_h(upd, + "prepare update (target %s, source %s, optstr %s)", + mnt_fs_get_target(upd->fs), + mnt_fs_get_source(upd->fs), mnt_fs_get_optstr(upd->fs))); if (!upd->filename) { @@ -621,18 +618,15 @@ int mnt_prepare_update(mnt_update *upd) goto err; } - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: prepare update: success\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "prepare update: success")); free(u); return 0; err: - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: prepare update: failed\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "prepare update: failed")); free(u); return -1; nothing: - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: prepare update: unnecessary\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "prepare update: unnecessary")); free(u); return 1; } @@ -644,8 +638,8 @@ static int add_entry(mnt_update *upd) assert(upd); - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: add entry\n", upd)); + DBG(MTAB, mnt_debug_h(upd, "add entry")); + if (upd->lc) mnt_lock_file(upd->lc); f = fopen(upd->filename, "a+"); @@ -674,8 +668,7 @@ static int remove_entry(mnt_update *upd) target = mnt_fs_get_target(upd->fs); assert(target); - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: remove entry (target %s)\n", upd, target)); + DBG(MTAB, mnt_debug_h(upd, "remove entry (target %s)", target)); if (upd->lc) mnt_lock_file(upd->lc); @@ -713,8 +706,7 @@ static int modify_target(mnt_update *upd) if (!upd->old_target) return -1; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: modify target (%s->%s)\n", upd, + DBG(MTAB, mnt_debug_h(upd, "modify target (%s->%s)", upd->old_target, mnt_fs_get_target(upd->fs))); if (upd->lc) @@ -749,8 +741,7 @@ static int modify_options(mnt_update *upd) assert(target); assert(upd->filename); - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: modify options (target %s)\n", upd, target)); + DBG(MTAB, mnt_debug_h(upd, "modify options (target %s)", target)); if (upd->lc) mnt_lock_file(upd->lc); @@ -797,8 +788,7 @@ int mnt_update_file(mnt_update *upd) if (!upd || !upd->fs) return -1; - DBG(DEBUG_MTAB, fprintf(stderr, - "libmount: update %p: update (target %s)\n", upd, + DBG(MTAB, mnt_debug_h(upd, "update (target %s)", mnt_fs_get_target(upd->fs))); /* * umount diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index 1947643a..99b7d5e5 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -410,8 +410,7 @@ char *mnt_get_mountpoint(const char *path) memcpy(mnt, "/", 2); done: - DBG(DEBUG_UTILS, fprintf(stderr, - "libmount: utils: fs-root for %s is %s\n", path, mnt)); + DBG(UTILS, mnt_debug("fs-root for %s is %s", path, mnt)); return mnt; err: free(mnt);